如何使用ROW_NUMBER()在过程中分页数据

时间:2012-11-05 06:03:27

标签: sql-server paging

我是sql server的首发,我写这个查询

ALTER PROCEDURE [dbo].[SPSelectReport3] (@StringWhereParameter nvarchar(4000)=null)
AS
BEGIN

    SET NOCOUNT ON;

-- َ Begin Of Transaction
begin tran

declare @Query nvarchar(max)
set @Query='
((SELECT Id,[Mesc]
      ,[Line]
      ,[Unit]
      ,[Discription]
      ,[InvQty]
      ,[LastDateNil]
      ,[ST_CODE]
      ,[PlanCode]
      ,[Min]
      ,[Max]
      ,[PbsNo]
      ,[PbsDate]
      ,[PbsQty]
      ,[PbsQtyRec]
      ,[DateDelay]
      ,[PartNo]
      ,[TranQty]
      ,[TypeRequest]
      ,[HeaderId]
  FROM [MyMaterialDB].[dbo].[Report3]
  WHERE headerid IN(SELECT HeaderId FROM [MyMaterialDB].[dbo].[Report3] WHERE line=''H'''+ @StringWhereParameter+'))
  UNION
  (
    (SELECT Id,[Mesc]
      ,[Line]
      ,[Unit]
      ,[Discription]
      ,[InvQty]
      ,[LastDateNil]
      ,[ST_CODE]
      ,[PlanCode]
      ,[Min]
      ,[Max]
      ,[PbsNo]
      ,[PbsDate]
      ,[PbsQty]
      ,[PbsQtyRec]
      ,[DateDelay]
      ,[PartNo]
      ,[TranQty]
      ,[TypeRequest]
      ,[HeaderId]
  FROM [MyMaterialDB].[dbo].[Report3]
  WHERE mesc IN(SELECT mesc FROM [MyMaterialDB].[dbo].[Report3] WHERE line=''I''' +@StringWhereParameter+'))
  UNION
  (SELECT Id,[Mesc]
      ,[Line]
      ,[Unit]
      ,[Discription]
      ,[InvQty]
      ,[LastDateNil]
      ,[ST_CODE]
      ,[PlanCode]
      ,[Min]
      ,[Max]
      ,[PbsNo]
      ,[PbsDate]
      ,[PbsQty]
      ,[PbsQtyRec]
      ,[DateDelay]
      ,[PartNo]
      ,[TranQty]
      ,[TypeRequest]
      ,[HeaderId]
  FROM [MyMaterialDB].[dbo].[Report3]
  WHERE mesc IN(SELECT HeaderId FROM [MyMaterialDB].[dbo].[Report3] WHERE line=''I'''+@StringWhereParameter+')
  )))
  Order by Mesc,Line,unit'


  exec(@Query)


 if @@error = 0    
 Commit Tran    
 Else   
 rollback tran
End

我写这个字符串Query和Get Where参数和concat Query以及在那之后运行Query。我想要分页结果数据,但我不知道如何进行分页。
请帮助我。谢谢大家。

1 个答案:

答案 0 :(得分:1)

使用整个事物(减去ORDER BY)作为派生表并在外部查询中应用ROW_NUMBER()(然后通过最外层查询中指定的行号进行过滤)应该没有问题。但我可能会先重写这个查询,也许是这样的:

'SELECT Id,[Mesc]
      ,[Line]
      ,[Unit]
      ,[Discription]
      ,[InvQty]
      ,[LastDateNil]
      ,[ST_CODE]
      ,[PlanCode]
      ,[Min]
      ,[Max]
      ,[PbsNo]
      ,[PbsDate]
      ,[PbsQty]
      ,[PbsQtyRec]
      ,[DateDelay]
      ,[PartNo]
      ,[TranQty]
      ,[TypeRequest]
      ,[HeaderId]
FROM [MyMaterialDB].[dbo].[Report3] t
WHERE EXISTS (
  SELECT *
  FROM [MyMaterialDB].[dbo].[Report3]
  WHERE (Line = ''H'' AND HeaderId = t.HeaderId
      OR Line = ''I'' AND t.Mesc IN (Mesc, HeaderId)
  ) ' + @StringWhereParameter + '
)'

现在添加行号列应该很容易:

'SELECT Id,[Mesc]
      ,[Line]
      ,[Unit]
      ,[Discription]
      ,[InvQty]
      ,[LastDateNil]
      ,[ST_CODE]
      ,[PlanCode]
      ,[Min]
      ,[Max]
      ,[PbsNo]
      ,[PbsDate]
      ,[PbsQty]
      ,[PbsQtyRec]
      ,[DateDelay]
      ,[PartNo]
      ,[TranQty]
      ,[TypeRequest]
      ,[HeaderId]
      ,ROW_NUMBER() OVER (ORDER BY  Mesc, Line, Unit) AS rn
FROM [MyMaterialDB].[dbo].[Report3] t
WHERE EXISTS (
  SELECT *
  FROM [MyMaterialDB].[dbo].[Report3]
  WHERE (Line = ''H'' AND HeaderId = t.HeaderId
      OR Line = ''I'' AND t.Mesc IN (Mesc, HeaderId)
  ) ' + @StringWhereParameter + '
)'

然后对其进行过滤

'SELECT Id,[Mesc]
      ,[Line]
      ,[Unit]
      ,[Discription]
      ,[InvQty]
      ,[LastDateNil]
      ,[ST_CODE]
      ,[PlanCode]
      ,[Min]
      ,[Max]
      ,[PbsNo]
      ,[PbsDate]
      ,[PbsQty]
      ,[PbsQtyRec]
      ,[DateDelay]
      ,[PartNo]
      ,[TranQty]
      ,[TypeRequest]
      ,[HeaderId]
FROM (
      SELECT Id,[Mesc]
            ,[Line]
            ,[Unit]
            ,[Discription]
            ,[InvQty]
            ,[LastDateNil]
            ,[ST_CODE]
            ,[PlanCode]
            ,[Min]
            ,[Max]
            ,[PbsNo]
            ,[PbsDate]
            ,[PbsQty]
            ,[PbsQtyRec]
            ,[DateDelay]
            ,[PartNo]
            ,[TranQty]
            ,[TypeRequest]
            ,[HeaderId]
            ,ROW_NUMBER() OVER (ORDER BY  Mesc, Line, Unit) AS rn
      FROM [MyMaterialDB].[dbo].[Report3] t
      WHERE EXISTS (
        SELECT *
        FROM [MyMaterialDB].[dbo].[Report3]
        WHERE (Line = ''H'' AND HeaderId = t.HeaderId
            OR Line = ''I'' AND t.Mesc IN (Mesc, HeaderId)
        ) ' + @StringWhereParameter + '
      )
) s
WHERE rn BETWEEN @offset + 1 AND @offset + @pagesize
;'