【order by case when b.ID is null then 0 else 1 end,a.CreateTime desc】
ALTER PROCEDURE [dbo].[up_Tb_ServiceWork_Paged]
@pagesize int, --每页显示的记录数
@pageindex int, --当前页索引,最小值为1
@RecordCount int output, --总记录数,<0时不统计结果记录
@ProName nvarchar(60),
@CustName nvarchar(60),
@LinkUser nvarchar(20),
@BeginTime datetime,
@EndTime datetime,
@CheckState int,
@BackState int,
@Isdel int,
@UserID nvarchar(40),
@return int
AS
set nocount on
declare @indextable table(rowid int identity(1,1),nid nvarchar(40))
insert into @indextable(nid)--将符合的记录插入到临时表中
select a.ID from dbo.Tb_ServiceApply a
left join dbo.Tb_Project b on a.ProID=b.ProID
left join dbo.Tb_Customer c on c.ID=a.CustName
where b.ProName like '%'+@ProName+'%'
and c.CustName like '%'+@CustName+'%'
and a.LinkUser like '%'+@LinkUser+'%'
and ((a.ApplyDate between @BeginTime+' 00:00:00' and @EndTime+' 23:59:59') or(a.ApplyDate is null))
and a.CheckState=@CheckState
and a.Isdel=@Isdel
and a.BackState=@BackState
if(@RecordCount<0)
BEGIN
select @RecordCount=COUNT(1) from @indextable
END
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound--最多执行行数,若下面还有超过此值的行数,请注释此行
begin
select b.WorkDate,b.WorkType,b.Completion,ProName,d.ProCode
from Tb_ServiceApply a
inner join @indextable t on a.ID=t.nid
left join dbo.Tb_ServiceWork b on a.ID=b.ApplyID
left join dbo.Tb_SysDataDiction f on b.WorkType =f.ID
left join dbo.Tb_SysDataDiction g on b.Completion=g.ID
left join dbo.Tb_Project d on a.ProID=d.ProID
left join dbo.Tb_Customer h on h.ID=a.CustName
where a.ID=t.nid and t.rowid>@PageLowerBound and t.rowid<=@PageUpperBound
order by case when b.ID is null then 0 else 1 end,a.CreateTime desc
end