IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_prd_content]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_prd_content]
GO
create procedure [dbo].[mps_g_prd_content](
@cmpid int ,
@useruid int , 
@pctag int,
@pcid int
) as
begin
set nocount on
declare @prdctg1 table(pcid int) 
declare @prdctg table(nid int identity(1,1),pcid int,allcode varchar(500)) 

if (@pcid=0) 
insert into @prdctg1(pcid) select pc_id from mps_prdctg where pc_cmpid=@cmpid and pc_tag=@pctag 
else insert into @prdctg1(pcid) values(@pcid) 

insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg(pcid) select distinct pcid from @prdctg1 


select a.* from mps_prdctg a , @prdctg , mps_prdctg_users 
where pc_id=pcid and pp_cuid=@useruid and pp_pcid=pc_id 
order by pc_level,pc_code ,pc_order, pc_type 

set nocount off 
end