1

我是 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 的地方。我想要分页结果数据,但我不知道如何进行分页。
请帮我。谢谢大家。

4

1 回答 1

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
;'
于 2012-11-05T07:31:11.673 回答