IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_pcsubmenu]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_pcsubmenu]
GO
CREATE proc [dbo].[mps_g_pcsubmenu]
@cmpid int,
@roleid int,
@menutype varchar(30) ,
@terminal int 
as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

if ( exists (select 1 from  mps_menu where mn_cmpid=@cmpid and mn_roleid=@roleid and mn_terminal=@terminal and mn_flag=@menutype)) 
begin
select * from  mps_menu 
where mn_cmpid=@cmpid and mn_roleid=@roleid and mn_terminal=@terminal and mn_flag=@menutype order by mn_order  
end else 
begin
select * from  mps_menu 
where mn_cmpid=@cmpid and mn_roleid=0 and mn_terminal=@terminal and mn_flag=@menutype order by mn_order  
end 
set nocount off 
end