IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_picksource]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_picksource]
GO
create procedure [dbo].[mps_g_picksource](
@cmpid int ,
@mybcid int ,
@dpid int,
@useruid int,
@bizsid int ,  -- 如果bizsid<0 表示是从comefrom中获取
@myctgid int,
@objid bigint,
@key nvarchar(50) ,
@onlyemp int ,
@extparam1 nvarchar(100),
@extparam2 nvarchar(100),
@extparam3 nvarchar(100),
@extparam4 nvarchar(100),
@extparam5 nvarchar(100),
@extparam6 nvarchar(100),
@extparam7 nvarchar(100),
@extparam8 nvarchar(100),
@extparam9 nvarchar(100) 
)

as
begin 
set nocount on
declare @ttype varchar(20)
declare @tobjid int
declare @tobjid2 int
declare @stype varchar(20) 
declare @sctgid int 
declare @sctgid2 int 
declare @orderf varchar(200) 
declare @disp nvarchar(4000) 
declare @cond  nvarchar(4000)



declare @flag int 
set @flag = 0 
declare @ctid int 
set @ctid =0 

declare @tbid int 
declare @dbname varchar(50) 
declare @tbname varchar(50) 
declare @keyfld varchar(30)
declare @keyfld2 varchar(30)
declare @extcond nvarchar(4000) 

declare @tbname2 varchar(50) 

create table #ctids(ctid int , ord int) 
if (@bizsid>0) 
begin
select @ttype=bizs_type , @tobjid=bizs_bizid,@tobjid2=bizs_ord2tag,@stype=bizs_sourcetype , @sctgid=bizs_ctgid , @sctgid2=bizs_ctg2id,@orderf=bizs_orderfields,
@cond=bizs_condition , @disp=bizs_displayfield 
from mps_biztype_source where bizs_id=@bizsid 
end else 
begin
select @ttype='biz' , @tobjid=bs_bizid,@tobjid2=bs_ord2tag,@stype=bs_stype , @sctgid=bs_sbizid , 
@sctgid2=bs_sord2tag,@orderf=bs_orderfields,
@cond=bs_sql , @disp=bs_displayfield 
from mps_biztype_comefrom where bs_id=(-1)*@bizsid 
end 
if (@sctgid>=100000000) 
begin
set @flag = @sctgid / 100000000 
set @sctgid = @sctgid - @flag * 100000000 
end 
if ((@flag>=5 and @flag<10) or (@flag>=15))
begin
--set @sctgid = @sctgid - @flag * 100000000 
set @ctid = @sctgid 
if (@stype='cst') 
begin
select @sctgid = ct_tag from mps_csttype where ct_id=@sctgid 
end 
if (@stype='prd') 
begin
select @sctgid = pc_tag from mps_prdctg where pc_id=@sctgid 
end 
if (@stype='equ') 
begin
select @sctgid = ec_tag from mps_equctg where ec_id=@sctgid 
end 
if (@stype='etc') 
begin
select @sctgid = etc_tag from mps_ext_resource where etc_id=@sctgid 
end 
end 
set @stype = lower(@stype) 
declare @sql nvarchar(4000) 
declare @i int 
declare @s1 nvarchar(1000) 

-- cu_code|员工编号||center;cu_depart|部门名称||left;cu_name|员工姓名||left;cu_title|岗位||leftcu_name,cu_title
declare @disp1 nvarchar(4000)
set @disp1 = '' 
while (@disp>'') 
begin
set @i = charindex(';',@disp) 
if (@i>0) 
begin
set @s1 = substring(@disp,1,@i-1) 
set @disp = substring(@disp,@i+1,len(@disp)-@i) 
end else 
begin
set @s1 = @disp 
set @disp ='' 
end
if (@s1>'') 
begin
set @i = charindex('|',@s1) 
if (@i>0) 
begin
set @s1 = substring(@s1,1,@i-1) 
if (@s1<>'selectedamount' and @s1<>'availableamount') 
begin
if (@disp1>'') set @disp1=@disp1 + ' or ' 
set @disp1 = @disp1 + @s1 +' like ''%''+@key+''%''' 
end 
end
end
end 
if (@disp1>'') set @disp1 = ' ('+@disp1+') ' 

if (@stype='web') 
begin
-- @sctgid=webservice svc_id 
set @sql = 'select top 200 svc_id as objid , 0 objid2 , * from webservice_'+LTRIM(rtrim(str(@sctgid)))+' where svc_cmpid='+ltrim(rtrim(str(@cmpid)))  +
' and svc_bcid='+rtrim(ltrim(str(@mybcid)))+' and svc_dpid='+rtrim(ltrim(str(@dpid))) 
if (@disp1>'') set @sql  = @sql+' and '+@disp1 
   if (@orderf>'') set @sql = @sql +' order by ' + @orderf else set @sql = @sql +' order by svc_id '
end 

if (@stype='emp') 
begin
-- @sctgid=-1/0/所有人  0/0/集团总部所有人  -2/0/当前分公司所有人 -3/0/当前部门所有人  
set @sql = 'select top 200 cu_id as objid , 0 objid2 , * from mps_employee where cu_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cu_use=0 ' 
if (@disp1>'') set @sql  = @sql+' and '+@disp1 
if (@sctgid=0)  --集团总部所有人
begin
set @sql = @sql +' and (cu_dpid=@dpid or @dpid=0) and cu_bcid=0 '
end else 
if (@sctgid=-2)  -- -2/0/当前分公司所有人
begin
set @sql = @sql +' and (cu_dpid=@dpid or @dpid=0) and cu_bcid='+rtrim(ltrim(str(@mybcid)))+' ' 
   end else 
   if (@sctgid=-3)  -- -3/0/当前部门所有人  
   begin
   set @sql = @sql +' and cu_bcid='+rtrim(ltrim(str(@mybcid)))+' and cu_dpid='+ rtrim(ltrim(str(@dpid)))+' ' 
   end else 
   begin
   set @sql = @sql +' and (cu_dpid=@dpid or @dpid=0) and cu_bcid=@bcid '
   end
   
   if (@orderf>'') set @sql = @sql +' order by ' + @orderf else set @sql = @sql +' order by cu_name '
   
   if (@sctgid=0)  --集团总部所有人
begin
exec sp_executesql @sql, N'@key nvarchar(50),@dpid int', @key  , @myctgid 
if (@onlyemp>0) select dp_fullname caption , 0 amount , dp_id value     from mps_depart where dp_cmpid=@cmpid and dp_bcid=0 and dp_fullname>'' order by dp_fullname 
end else 
if (@sctgid=-2)  -- -2/0/当前分公司所有人
begin
exec sp_executesql @sql, N'@key nvarchar(50),@dpid int', @key  , @myctgid 
if (@onlyemp>0) select dp_fullname caption , 0 amount , dp_id value     from mps_depart where dp_cmpid=@cmpid and dp_bcid=@mybcid and dp_fullname>'' order by dp_fullname 
   end else 
   if (@sctgid=-3)  -- -3/0/当前部门所有人  
   begin
   exec sp_executesql @sql, N'@key nvarchar(50)', @key  
if (@onlyemp>0) select '' caption , 0 amount , 0 value    where 0=1 
   end else
   begin
   exec sp_executesql @sql, N'@key nvarchar(50),@dpid int,@bcid int', @key , @myctgid,@mybcid 
   if (@onlyemp>0) select dp_fullname caption , 0 amount , dp_id value     from mps_depart where dp_cmpid=@cmpid and dp_bcid=@mybcid and dp_fullname>'' order by dp_fullname 
   end
end  
-- 完全自定义的
if (@stype='private') 
begin
-- @sctgid 对应的是 ps_privatesql 中的 ps_id 
select @extcond = ps_extcond from mps_privatesql where ps_id=@sctgid 
set @extcond=isnull(@extcond,'') 

set @tbname = 'privateview_'+ltrim(rtrim(str(@sctgid))) 
set @keyfld='objid' 
set @sql = 'select top 200 0 objid2, * from '+@tbname+' ' 

if (len(@extcond)>10) 
begin
if (charindex(' where ',@sql)<=0) set @sql  = @sql+' where   ('+@extcond+') '  else set @sql  = @sql+' and ('+@extcond+') '  
end

if (@cond>'') 
begin
if (charindex(' where ',@sql)<=0) set @sql  = @sql+' where   ('+@cond+') '  
else set @sql  = @sql+' and ('+@cond+') '  
end else 
begin
if (@disp1>'') 
begin
if (charindex(' where ',@sql)<=0) set @sql  = @sql+' where   ('+@disp1+') '  
else set @sql  = @sql+' and ('+@disp1+') '  
end 
end 



if (len(@orderf)>5) set  @sql  = @sql+' order by  ' + @orderf 

exec sp_executesql @sql, N'@key nvarchar(50),@bcid int, @useruid int,@dpid int , @bizid int , 
@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),
@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),
@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100)', 
@key  , @mybcid,@myctgid , @useruid,@sctgid , 
@extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9 
end 
 
if (@stype='biz') 
begin
exec sys_sql_getTableNamePk @cmpid,'biz',@sctgid,0,@tbname output , @keyfld output , @dbname output 
set @keyfld='ord_id' 
set @keyfld2='ord2_id' 
set @tbname = @dbname+@tbname 
set @tbname2 = '' 
if (@sctgid2>0) 
begin
set @tbname2 = @tbname+'_'+ltrim(rtrim(str(@sctgid2))) 
set @keyfld2='ord2_id' 
end
if (@sctgid2<=0) 
begin
set @sql = 'select top 200 ord_id as objid , 0 objid2, * from '+@tbname+' where ord_cmpid='+ltrim(rtrim(str(@cmpid)))  +'  ' 
end else 
begin
set @sql = 'select top 200 ord2_ordid as objid2 , ord2_id objid, a.* from '+@tbname+' b  , '+@tbname2+' a  where ord_id=ord2_ordid  ' 
end 


if (@cond>'') 
begin
set @sql  = @sql+' and ('+@cond+') '  
end else 
begin
if (@disp1>'') set @sql  = @sql+' and ('+@disp1+') ' 
end 



if (@orderf>'') set  @sql  = @sql+' order by  ' + @orderf 

exec sp_executesql @sql, N'@key nvarchar(50),@bcid int, @useruid int,@dpid int , @bizid int , 
@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),
@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),
@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100)', 
@key  , @mybcid,@myctgid , @useruid,@sctgid , 
@extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9 
end
if (@stype='cst') 
begin
exec sys_sql_getTableNamePk @cmpid,'cst',@sctgid,0,@tbname output , @keyfld output , @dbname output 
set @keyfld='cc_id' 
set @keyfld2='cc2_id' 
set @tbname = @dbname+@tbname 
set @tbname2 = '' 
if (@sctgid2>0) 
begin
set @tbname2 = @tbname+'_'+ltrim(rtrim(str(@sctgid2))) 
end
if (@sctgid2<=0) 
begin
if (@flag<=1)   -- all 
set @sql = 'select top 200 cc_id as objid , 0 objid2, * from '+@tbname+' where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_tag='+convert(varchar(10),@sctgid)+'  ' 
if (@flag=2)  -- branch 
set @sql = 'select top 200 cc_id as objid , 0 objid2, * from '+@tbname+' where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_tag='+convert(varchar(10),@sctgid)+' and cc_bcid=' + convert(varchar(10),@mybcid)+' ' 
if (@flag=3) -- depart 
set @sql = 'select top 200 cc_id as objid , 0 objid2, * from '+@tbname+' where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_tag='+convert(varchar(10),@sctgid)+' and cc_dpid=' + convert(varchar(10),@dpid)+' ' 
if (@flag=4) -- my  
set @sql = 'select top 200 cc_id as objid , 0 objid2, * from '+@tbname+' where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_tag='+convert(varchar(10),@sctgid)+' and cc_cuid=' + convert(varchar(10),@useruid)+' ' 
if (@flag=5)   -- all 
set @sql = 'select top 200 cc_id as objid , 0 objid2, * from '+@tbname+' where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_ctid='+convert(varchar(10),@ctid)+'  ' 
if (@flag=6)  -- branch 
set @sql = 'select top 200 cc_id as objid , 0 objid2, * from '+@tbname+' where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_ctid='+convert(varchar(10),@ctid)+' and cc_bcid=' + convert(varchar(10),@mybcid)+' ' 
if (@flag=7) -- depart 
set @sql = 'select top 200 cc_id as objid , 0 objid2, * from '+@tbname+' where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_ctid='+convert(varchar(10),@ctid)+' and cc_dpid=' + convert(varchar(10),@dpid)+' ' 
if (@flag=8) -- my  
set @sql = 'select top 200 cc_id as objid , 0 objid2, * from '+@tbname+' where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_ctid='+convert(varchar(10),@ctid)+' and cc_cuid=' + convert(varchar(10),@useruid)+' ' 
end else 
begin
if (@flag<=1)   -- all 
set @sql = 'select top 200 cc2_ccid as objid2 , cc2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_id=cc2_ccid and cc_tag='+convert(varchar(10),@sctgid)+'  ' 
if (@flag=2)  -- branch 
set @sql = 'select top 200 cc2_ccid as objid2 , cc2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_id=cc2_ccid and cc_tag='+convert(varchar(10),@sctgid)+' and cc_bcid=' + convert(varchar(10),@mybcid)+' ' 
if (@flag=3) -- depart 
set @sql = 'select top 200 cc2_ccid as objid2 , cc2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_id=cc2_ccid and cc_tag='+convert(varchar(10),@sctgid)+' and cc_dpid=' + convert(varchar(10),@dpid)+' ' 
if (@flag=4) -- my  
set @sql = 'select top 200 cc2_ccid as objid2 , cc2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_id=cc2_ccid and cc_tag='+convert(varchar(10),@sctgid)+' and cc_cuid=' + convert(varchar(10),@useruid)+' ' 
if (@flag=5)   -- all 
set @sql = 'select top 200 cc2_ccid as objid2 , cc2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_id=cc2_ccid and cc_ctid='+convert(varchar(10),@ctid)+'  ' 
if (@flag=6)  -- branch 
set @sql = 'select top 200 cc2_ccid as objid2 , cc2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_id=cc2_ccid and cc_ctid='+convert(varchar(10),@ctid)+' and cc_bcid=' + convert(varchar(10),@mybcid)+' ' 
if (@flag=7) -- depart 
set @sql = 'select top 200 cc2_ccid as objid2 , cc2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_id=cc2_ccid and cc_ctid='+convert(varchar(10),@ctid)+' and cc_dpid=' + convert(varchar(10),@dpid)+' ' 
if (@flag=8) -- my  
set @sql = 'select top 200 cc2_ccid as objid2 , cc2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cc_id=cc2_ccid and cc_ctid='+convert(varchar(10),@ctid)+' and cc_cuid=' + convert(varchar(10),@useruid)+' ' 
end 

if (@cond>'') 
begin
set @sql  = @sql+' and ('+@cond+') '  
end else 
begin
if (@disp1>'') set @sql  = @sql+' and ('+@disp1+') ' 
end 
 


if (@orderf>'') set  @sql  = @sql+' order by  ' + @orderf 

exec sp_executesql @sql, N'@key nvarchar(50),@bcid int, @useruid int,@dpid int , @bizid int , 
@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),
@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),
@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100)', 
@key  , @mybcid,@myctgid , @useruid,@sctgid , 
@extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9 
end

if (@stype='prd') 
begin
exec sys_sql_getTableNamePk @cmpid,'prd',@sctgid,0,@tbname output , @keyfld output , @dbname output 
set @keyfld='prd_id' 
set @keyfld2='prd2_id' 
set @tbname = @dbname+@tbname 
set @tbname2 = '' 
if (@sctgid2>0) 
begin
set @tbname2 = @tbname+'_'+ltrim(rtrim(str(@sctgid2))) 
end

if (@myctgid>0)
begin
set @sql = ' select prd_id as objid , 0 objid2,* from '+@dbname+@tbname+' where prd_title >'''' and prd_pcid='+convert(varchar(50),@myctgid)
exec sp_executesql @sql
return
end else
if (@flag>=5) 
begin
 insert into #ctids(ctid,ord) values(@ctid,0) 
 insert into #ctids(ctid,ord) select pc_id,1 from mps_prdctg where pc_cmpid=@cmpid and pc_father=@ctid 
 insert into #ctids(ctid,ord) select pc_id,2 from mps_prdctg,#ctids where pc_cmpid=@cmpid and pc_father=ctid and ord=1 
 insert into #ctids(ctid,ord) select pc_id,3 from mps_prdctg,#ctids where pc_cmpid=@cmpid and pc_father=ctid and ord=2 
 insert into #ctids(ctid,ord) select pc_id,4 from mps_prdctg,#ctids where pc_cmpid=@cmpid and pc_father=ctid and ord=3 
 insert into #ctids(ctid,ord) select pc_id,5 from mps_prdctg,#ctids where pc_cmpid=@cmpid and pc_father=ctid and ord=4 
end 


if (@sctgid2<=0) 
begin
if (@flag<=1)   -- all 
set @sql = 'select top 200 prd_id as objid , 0 objid2, * from '+@tbname+' where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_tag='+convert(varchar(10),@sctgid)+'  ' 
if (@flag=2)  -- branch 
set @sql = 'select top 200 prd_id as objid , 0 objid2, * from '+@tbname+' where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_tag='+convert(varchar(10),@sctgid)+' and prd_bcid=' + convert(varchar(10),@mybcid)+' ' 
if (@flag=3) -- depart 
set @sql = 'select top 200 prd_id as objid , 0 objid2, * from '+@tbname+' where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_tag='+convert(varchar(10),@sctgid)+' and prd_dpid=' + convert(varchar(10),@dpid)+' ' 
if (@flag=4) -- my  
set @sql = 'select top 200 prd_id as objid , 0 objid2, * from '+@tbname+' where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_tag='+convert(varchar(10),@sctgid)+' and prd_cuid=' + convert(varchar(10),@useruid)+' ' 
if (@flag=5)   -- all 
set @sql = 'select top 200 prd_id as objid , 0 objid2, a.* from '+@tbname+' a,#ctids where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_pcid=ctid  ' 
if (@flag=6)  -- branch 
set @sql = 'select top 200 prd_id as objid , 0 objid2, a.* from '+@tbname+' a,#ctids where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_pcid=ctid and prd_bcid=' + convert(varchar(10),@mybcid)+' ' 
if (@flag=7) -- depart 
set @sql = 'select top 200 prd_id as objid , 0 objid2, a.* from '+@tbname+' a,#ctids where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_pcid=ctid and prd_dpid=' + convert(varchar(10),@dpid)+' ' 
if (@flag=8) -- my  
set @sql = 'select top 200 prd_id as objid , 0 objid2, a.* from '+@tbname+' a,#ctids where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_pcid=ctid and prd_cuid=' + convert(varchar(10),@useruid)+' ' 
end else 
begin
if (@flag<=1)   -- all 
set @sql = 'select top 200 prd2_prdid as objid2 , prd2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_id=prd2_prdid and prd_tag='+convert(varchar(10),@sctgid)+'  ' 
if (@flag=2)  -- branch 
set @sql = 'select top 200 prd2_prdid as objid2 , prd2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_id=prd2_prdid and prd_tag='+convert(varchar(10),@sctgid)+' and prd_bcid=' + convert(varchar(10),@mybcid)+' ' 
if (@flag=3) -- depart 
set @sql = 'select top 200 prd2_prdid as objid2 , prd2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_id=prd2_prdid and prd_tag='+convert(varchar(10),@sctgid)+' and prd_dpid=' + convert(varchar(10),@dpid)+' ' 
if (@flag=4) -- my  
set @sql = 'select top 200 prd2_prdid as objid2 , prd2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_id=prd2_prdid and prd_tag='+convert(varchar(10),@sctgid)+' and prd_cuid=' + convert(varchar(10),@useruid)+' ' 
if (@flag=5)   -- all 
set @sql = 'select top 200 prd2_prdid as objid2 , prd2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b,#ctids where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_id=prd2_prdid and prd_pcid=ctid  ' 
if (@flag=6)  -- branch 
set @sql = 'select top 200 prd2_prdid as objid2 , prd2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b,#ctids where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_id=prd2_prdid and prd_pcid=ctid and prd_bcid=' + convert(varchar(10),@mybcid)+' ' 
if (@flag=7) -- depart 
set @sql = 'select top 200 prd2_prdid as objid2 , prd2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b,#ctids where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_id=prd2_prdid and prd_pcid=ctid and prd_dpid=' + convert(varchar(10),@dpid)+' ' 
if (@flag=8) -- my  
set @sql = 'select top 200 prd2_prdid as objid2 , prd2_id objid, b.* from '+@tbname+' a  , '+@tbname2+' b,#ctids where prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and prd_id=prd2_prdid and prd_pcid=ctid and prd_cuid=' + convert(varchar(10),@useruid)+' ' 
end 

if (@cond>'') 
begin
set @sql  = @sql+' and ('+@cond+') '  
end else 
begin
if (@disp1>'') set @sql  = @sql+' and ('+@disp1+') ' 
end 
 


if (@orderf>'') set  @sql  = @sql+' order by  ' + @orderf 

exec sp_executesql @sql, N'@key nvarchar(50),@bcid int, @useruid int,@dpid int , @bizid int , 
@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),
@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),
@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100)', 
@key  , @mybcid,@myctgid , @useruid,@sctgid , 
@extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9 

if(@ctid>0)
begin
select pc_full caption ,pc_id value from mps_prdctg 
where pc_father = @ctid or pc_id = @ctid
order by pc_level,pc_order
end else 
begin
select pc_full caption ,pc_id value from mps_prdctg 
where pc_tag = @sctgid
order by pc_level,pc_order
select @sctgid as aa
end

end
drop table #ctids
set nocount off
end