IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_document_management]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_document_management]
GO
create proc [dbo].[mps_document_management] 
@cmpid int ,
@dcid int,
@key1 nvarchar(50),
@key2 nvarchar(50),
@key3 nvarchar(50),
@key4 nvarchar(50),
@key5 nvarchar(50),
@pagesize int ,
@pagecount int,
@pageobjids varchar(8000) 
as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @amount int 
set @amount=@pagecount*@pagesize

declare @pobjs table(nid int identity(1,1) , lgid bigint)
declare @s1 varchar(8000)
declare @s2 varchar(100) 
declare @i int
set @s1 = @pageobjids 
if (@s1='_none_') set @s1=''
while (@s1>'') 
begin
  set @i=charindex(',',@s1)
  if (@i>0)
  begin
    set @s2 = substring(@s1,1,@i-1) 
    set @s1 = substring(@s1,@i+1,len(@s1)-@i)
  end else 
  begin
    set @s2 = @s1 
    set @s1 = '' 
  end 
  if (@s2>'') insert into @pobjs(lgid) values(convert(bigint,@s2)) 
end

if (exists (select * from @pobjs))
begin
select * from mps_document a , @pobjs where doc_id=lgid order by nid 
return ;
end


select count(1) as reccount 
from mps_document 
where doc_cmpid=@cmpid and doc_dcid=@dcid and 
(@key1='' or (doc_title like '%'+@key1+'%' or  doc_abstract  like '%'+@key1+'%' or doc_label like '%'+@key1+'%' or doc_source  like '%'+@key1+'%' ))  and 
(@key2='' or (doc_title like '%'+@key2+'%' or  doc_abstract  like '%'+@key2+'%' or doc_label like '%'+@key2+'%' or doc_source  like '%'+@key2+'%' ))  and 
(@key3='' or (doc_title like '%'+@key3+'%' or  doc_abstract  like '%'+@key3+'%' or doc_label like '%'+@key3+'%' or doc_source  like '%'+@key3+'%' ))  and 
(@key4='' or (doc_title like '%'+@key4+'%' or  doc_abstract  like '%'+@key4+'%' or doc_label like '%'+@key4+'%' or doc_source  like '%'+@key4+'%' ))  and 
(@key5='' or (doc_title like '%'+@key5+'%' or  doc_abstract  like '%'+@key5+'%' or doc_label like '%'+@key5+'%' or doc_source  like '%'+@key5+'%' ))   



select top(@amount) doc_id from mps_document 
where doc_cmpid=@cmpid and doc_dcid=@dcid and 
(@key1='' or (doc_title like '%'+@key1+'%' or  doc_abstract  like '%'+@key1+'%' or doc_label like '%'+@key1+'%' or doc_source  like '%'+@key1+'%' ))  and 
(@key2='' or (doc_title like '%'+@key2+'%' or  doc_abstract  like '%'+@key2+'%' or doc_label like '%'+@key2+'%' or doc_source  like '%'+@key2+'%' ))  and 
(@key3='' or (doc_title like '%'+@key3+'%' or  doc_abstract  like '%'+@key3+'%' or doc_label like '%'+@key3+'%' or doc_source  like '%'+@key3+'%' ))  and 
(@key4='' or (doc_title like '%'+@key4+'%' or  doc_abstract  like '%'+@key4+'%' or doc_label like '%'+@key4+'%' or doc_source  like '%'+@key4+'%' ))  and 
(@key5='' or (doc_title like '%'+@key5+'%' or  doc_abstract  like '%'+@key5+'%' or doc_label like '%'+@key5+'%' or doc_source  like '%'+@key5+'%' ))  
order by doc_create 

select top(@pagesize) * from mps_document 
where doc_cmpid=@cmpid and doc_dcid=@dcid and 
(@key1='' or (doc_title like '%'+@key1+'%' or  doc_abstract  like '%'+@key1+'%' or doc_label like '%'+@key1+'%' or doc_source  like '%'+@key1+'%' ))  and 
(@key2='' or (doc_title like '%'+@key2+'%' or  doc_abstract  like '%'+@key2+'%' or doc_label like '%'+@key2+'%' or doc_source  like '%'+@key2+'%' ))  and 
(@key3='' or (doc_title like '%'+@key3+'%' or  doc_abstract  like '%'+@key3+'%' or doc_label like '%'+@key3+'%' or doc_source  like '%'+@key3+'%' ))  and 
(@key4='' or (doc_title like '%'+@key4+'%' or  doc_abstract  like '%'+@key4+'%' or doc_label like '%'+@key4+'%' or doc_source  like '%'+@key4+'%' ))  and 
(@key5='' or (doc_title like '%'+@key5+'%' or  doc_abstract  like '%'+@key5+'%' or doc_label like '%'+@key5+'%' or doc_source  like '%'+@key5+'%' ))  
order by doc_create 

set nocount off
end