IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_attach_log]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_attach_log]
GO
create procedure [dbo].[mps_g_attach_log](
@daid bigint ,
@key nvarchar(100) ,
@operation int,
@pagesize int,
@pagecount int ,
@pageobjids varchar(8000) 
) as
begin
set nocount on 

declare @amount int 
declare @s1 varchar(8000) 
declare @s2 varchar(100) 
declare @i int  
select @amount = @pagecount*@pagesize 
declare @pobjs table(nid int identity(1, 1), ordid bigint) 
set @s1 = @pageobjids  
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  
  insert into @pobjs(ordid) values(convert(bigint,@s2)) 
end 
if (exists (select top 1 * from @pobjs)) 
begin 
select a.* from mps_attach_log a , @pobjs where du_id=ordid order by nid 
return 
end 

if (@key='') 
begin
select count(1) as reccount 
from mps_attach_log 
where du_daid=@daid   and du_operation=@operation

select top(@amount) du_id from mps_attach_log 
where du_daid=@daid and du_operation=@operation
order by du_lastdate 

select top(@pagesize) * from mps_attach_log 
where du_daid=@daid  and du_operation=@operation
order by du_lastdate 
end else 
begin
select count(1) as reccount 
from mps_attach_log 
where du_daid=@daid  and du_operation=@operation and (du_branch like '%'+@key+'%' or  du_depart like '%'+@key+'%' or  du_cuname like '%'+@key+'%') 

select top(@amount) du_id from mps_attach_log 
where du_daid=@daid  and du_operation=@operation and (du_branch like '%'+@key+'%' or  du_depart like '%'+@key+'%' or  du_cuname like '%'+@key+'%') 
order by du_lastdate 

select top(@pagesize) * from mps_attach_log 
where du_daid=@daid  and du_operation=@operation and (du_branch like '%'+@key+'%' or  du_depart like '%'+@key+'%' or  du_cuname like '%'+@key+'%') 
order by du_lastdate 
end 

set nocount off
end