IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_myworkinfo_task]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_myworkinfo_task]
GO
CREATE procedure [dbo].[mps_g_myworkinfo_task](
@cmpid int ,
@useruid int,
@objid bigint  ,
@key nvarchar(50) ,
@allcount int=default 
)
as 
begin
set nocount on
if (@allcount is null or @allcount<0) set @allcount = 100 

-- 如果 @allcount <= 20  or @key中有内容 ， 则不聚合




if (@allcount<=20 or @key>'') 
begin
select 'task' objtag , tsk_type objtype  , tsk_type objtitle1 , tsk_type objtitle2, 
1 objcount ,
tsk_id identid ,tsk_id objid , tsk_objtype objctgtype, tsk_ctgid objid1, tsk_objid objid2 ,
CONVERT(varchar(10),tsk_date,120)+' / '+tsk_message objtitle 
from mps_task  with(nolock) 
where tsk_cmpid=@cmpid and tsk_cuid=@useruid  and  
( tsk_depart like '%'+@key+'%' or tsk_cuname like '%'+@key+'%' or tsk_objtype like '%'+@key+'%' 
or tsk_message like '%'+@key+'%' or tsk_type like '%'+@key+'%' or tsk_branch like '%'+@key+'%')
order by tsk_id desc 
end else
begin
select 'task' objtag , tsk_type objtype  , min(tsk_message) objtitle1 , MAX(tsk_message) objtitle2, 
COUNT(tsk_id) objcount ,
min(tsk_id) identid ,min(tsk_id) objid , tsk_objtype objctgtype, min(tsk_ctgid) objid1,min(tsk_objid) objid2 ,
MIN(CONVERT(varchar(10),tsk_date,120)+' / '+tsk_message) objtitle 
from mps_task  with(nolock) 
where tsk_cmpid=@cmpid and tsk_cuid=@useruid  and  
( tsk_depart like '%'+@key+'%' or tsk_cuname like '%'+@key+'%' or tsk_objtype like '%'+@key+'%' 
or tsk_message like '%'+@key+'%' or tsk_type like '%'+@key+'%' or tsk_branch like '%'+@key+'%')
group by tsk_type  , tsk_objtype 
order by tsk_type  
end 
 
set nocount off
end