IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_csthome_md]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_csthome_md]
GO
 
CREATE procedure [dbo].[mps_g_csthome_md](
@cmpid int,
@ctid int , 
@ccid bigint,
@terminal int --1:mobile  0:pc   如果@mobile:0=mobile 1=pc 
)
as
begin
set nocount on
declare @mdtag varchar(100) 

set @mdtag='csthome' 

-- t1 模板
select * from mps_template WITH(nolock) where tmp_cmpid=@cmpid and tmp_type='csthome' and tmp_objid=@ctid 
-- t2 菜单
if (@terminal=0) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@ctid) and mn_terminal=0 and mn_roleid=0 order by mn_order 
end else 
begin
if (exists (select 1 from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@ctid) and mn_terminal=1)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@ctid) and mn_terminal=1   order by mn_order 
end  else 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@ctid) and mn_terminal=0   order by mn_order 
end 
end

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
declare @mctid int 
set @mctid = @ctid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'cst',@mctid,0,@tbname output,@fkey output ,@dbname output 

-- t3 客户主表信息
declare @sql nvarchar(4000) 
set @sql = 'select * from '+@dbname+@tbname+' with(nolock) where cc_cmpid=@cmpid and cc_id=@ccid ' 
exec sp_executesql @sql, N'@cmpid int ,@ccid bigint', @cmpid,@ccid 

-- t4 扩展表
set @sql = 'select * from '+@dbname+@tbname+'_0 with(nolock) where cc3_ccid=@ccid ' 
exec sp_executesql @sql, N'@cmpid int ,@ccid bigint', @cmpid,@ccid 

set nocount off
end