IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_1doc_similar]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_1doc_similar]
GO
create  procedure [dbo].[mps_g_1doc_similar](
@cmpid int,
@useruid int ,  
@usersecurity int,
    @docid bigint, 
    @dcid int ,
    @key1 nvarchar(50),
    @key2 nvarchar(50),
    @key3 nvarchar(50),
    @key4 nvarchar(50),
    @key5 nvarchar(50)
    ) 
as  
begin  
set nocount on 
declare @ic int 
set @ic=12 
declare @ic1 int 
set @ic1 = 0

declare @t table(id int IDENTITY(0,1) , docid bigint) 
if (@key1='')
begin
insert into @t(docid) select top(@ic) doc_id from mps_document 
where doc_cmpid=@cmpid and doc_dcid=@dcid and doc_security<=@usersecurity and doc_id<>@docid order by doc_create 
select @ic1=count(1) from @t 
set @ic1 = @ic - @ic1 
if (@ic1>0) insert into @t(docid) select top(@ic1) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity  and doc_id<>@docid order by doc_create 
end  else 
if (@key1>'' and @key2='')
begin
insert into @t(docid) select top(@ic) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity and 
(doc_title like '%'+@key1+'%' or 
doc_abstract like '%'+@key1+'%' or 
doc_label like '%'+@key1+'%' or 
doc_search like '%'+@key1+'%' )  and doc_id<>@docid 
order by case when doc_dcid=@dcid then 0 else 1 end , doc_create  

select @ic1=count(1) from @t 
set @ic1 = @ic - @ic1 
if (@ic1>0) 
insert into @t(docid) select top(@ic1) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity  and doc_id<>@docid order by doc_create 
end  else 
if (@key1>'' and @key2>'' and @key3='')
begin
insert into @t(docid) select top(@ic) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity and 
(doc_title like '%'+@key1+'%' or 
doc_abstract like '%'+@key1+'%' or 
doc_label like '%'+@key1+'%' or 
doc_search like '%'+@key1+'%' ) and 
(doc_title like '%'+@key2+'%' or 
doc_abstract like '%'+@key2+'%' or 
doc_label like '%'+@key2+'%' or 
doc_search like '%'+@key2+'%' ) and doc_id<>@docid 
order by case when doc_dcid=@dcid then 0 else 1 end , doc_create  

select @ic1=count(1) from @t 
set @ic1 = @ic - @ic1 
if (@ic1>0) 
insert into @t(docid) select top(@ic1) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity  and doc_id<>@docid order by doc_create 
end  else 
if (@key1>'' and @key2>'' and @key3>'' and @key4='')
begin
insert into @t(docid) select top(@ic) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity and 
(doc_title like '%'+@key1+'%' or 
doc_abstract like '%'+@key1+'%' or 
doc_label like '%'+@key1+'%' or 
doc_search like '%'+@key1+'%' ) and 
(doc_title like '%'+@key2+'%' or 
doc_abstract like '%'+@key2+'%' or 
doc_label like '%'+@key2+'%' or 
doc_search like '%'+@key2+'%' ) and 
(doc_title like '%'+@key3+'%' or 
doc_abstract like '%'+@key3+'%' or 
doc_label like '%'+@key3+'%' or 
doc_search like '%'+@key3+'%' ) and doc_id<>@docid 
order by case when doc_dcid=@dcid then 0 else 1 end , doc_create  

select @ic1=count(1) from @t 
set @ic1 = @ic - @ic1 
if (@ic1>0) 
insert into @t(docid) select top(@ic1) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity  and doc_id<>@docid order by doc_create 
end  else 
if (@key1>'' and @key2>'' and @key3>'' and @key4>'' and @key5='')
begin
insert into @t(docid) select top(@ic) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity and 
(doc_title like '%'+@key1+'%' or 
doc_abstract like '%'+@key1+'%' or 
doc_label like '%'+@key1+'%' or 
doc_search like '%'+@key1+'%' ) and 
(doc_title like '%'+@key2+'%' or 
doc_abstract like '%'+@key2+'%' or 
doc_label like '%'+@key2+'%' or 
doc_search like '%'+@key2+'%' ) and 
(doc_title like '%'+@key3+'%' or 
doc_abstract like '%'+@key3+'%' or 
doc_label like '%'+@key3+'%' or 
doc_search like '%'+@key3+'%' ) and 
(doc_title like '%'+@key4+'%' or 
doc_abstract like '%'+@key4+'%' or 
doc_label like '%'+@key4+'%' or 
doc_search like '%'+@key4+'%' ) and doc_id<>@docid 
order by case when doc_dcid=@dcid then 0 else 1 end , doc_create  

select @ic1=count(1) from @t 
set @ic1 = @ic - @ic1 
if (@ic1>0) 
insert into @t(docid) select top(@ic1) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity  and doc_id<>@docid order by doc_create 
end  else 
begin
insert into @t(docid) select top(@ic) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity and 
(doc_title like '%'+@key1+'%' or 
doc_abstract like '%'+@key1+'%' or 
doc_label like '%'+@key1+'%' or 
doc_search like '%'+@key1+'%' ) and 
(doc_title like '%'+@key2+'%' or 
doc_abstract like '%'+@key2+'%' or 
doc_label like '%'+@key2+'%' or 
doc_search like '%'+@key2+'%' ) and 
(doc_title like '%'+@key3+'%' or 
doc_abstract like '%'+@key3+'%' or 
doc_label like '%'+@key3+'%' or 
doc_search like '%'+@key3+'%' ) and 
(doc_title like '%'+@key4+'%' or 
doc_abstract like '%'+@key4+'%' or 
doc_label like '%'+@key4+'%' or 
doc_search like '%'+@key4+'%' ) or 
(doc_title like '%'+@key5+'%' or 
doc_abstract like '%'+@key5+'%' or 
doc_label like '%'+@key5+'%' or 
doc_search like '%'+@key5+'%' ) and doc_id<>@docid 
order by case when doc_dcid=@dcid then 0 else 1 end , doc_create  

select @ic1=count(1) from @t 
set @ic1 = @ic - @ic1 
if (@ic1>0) 
insert into @t(docid) select top(@ic1) doc_id from mps_document 
where doc_cmpid=@cmpid and  doc_security<=@usersecurity  and doc_id<>@docid order by doc_create 
end   
select id doc_order,a.* from mps_document a, @t 
where doc_id=docid 
order by id 
set nocount off
end