IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_doc_keys]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_doc_keys]
GO
CREATE procedure [dbo].[mps_doc_keys](
@cmpid int,
@dctag int ,
@dcid int 
) as
begin
set nocount on
declare @dc table(dcid int) 
if (@dcid>0) 
begin
insert into @dc(dcid) values(@dcid) 
end else 
begin
insert into @dc(dcid) select dc_id from mps_docctg where dc_cmpid=@cmpid and dc_tag=@dctag  
end 
-- 将子目录加入
insert into @dc(dcid) 
select dc_id from mps_docctg , @dc 
where dc_father = dcid and 
(not exists (select 1 from @dc where dcid=dc_id))

insert into @dc(dcid) 
select dc_id from mps_docctg , @dc 
where dc_father = dcid and 
(not exists (select 1 from @dc where dcid=dc_id))

insert into @dc(dcid) 
select dc_id from mps_docctg , @dc 
where dc_father = dcid and 
(not exists (select 1 from @dc where dcid=dc_id))

insert into @dc(dcid) 
select dc_id from mps_docctg , @dc 
where dc_father = dcid and 
(not exists (select 1 from @dc where dcid=dc_id))

select distinct dc_keys from mps_docctg , @dc where dc_keys != '' and dc_id=dcid 
set nocount off
end