SQL Server 2005相当于OFFSET / FETCH

时间:2013-01-10 18:04:50

标签: sql-server-2005

  

可能重复:
  LIMIT 10..20 in sqlserver

我尝试在SQL Server 2005中使用以下代码(它是在2012年编写的):

SELECT *
FROM  (SELECT TOP (100) PERCENT ID_BADGE,
                                DATE_TRX,
                                CODE_TRX,
                                SEQ_REC
       FROM   omi.DCUTRX_ZERO
       UNION ALL
       SELECT TOP (100) PERCENT ID_BADGE,
                                DATE_TRX,
                                CODE_TRX,
                                SEQ_REC
       FROM   omi.TAHIST_ZERO) OMI
WHERE  ( ID_BADGE = '     ' + @Badge )
       AND ( DATE_TRX BETWEEN @Date AND @Date2 )
       AND ( SEQ_REC IN ( '0', '1000' ) )
       AND ( CODE_TRX IN ( '100', '101' ) ) 
    OFFSET 1 ROWS 
    FETCH NEXT 2 ROWS ONLY 

有人能告诉我SQL Server 2005的等价物吗?

1 个答案:

答案 0 :(得分:2)

SELECT  ID_BADGE, DATE_TRX, CODE_TRX, SEQ_REC
FROM    (
        SELECT  *, ROW_NUMBER() OVER (ORDER BY DATE_TRX) rn
        FROM    (
                SELECT  ID_BADGE, DATE_TRX, CODE_TRX, SEQ_REC
                FROM    omi.DCUTRX_ZERO
                UNION ALL
                SELECT  ID_BADGE, DATE_TRX, CODE_TRX, SEQ_REC
                FROM    omi.TAHIST_ZERO
                ) q
       WHERE    ID_BADGE = '     ' + @Badge
                AND DATE_TRX BETWEEN @Date AND @Date2
                AND SEQ_REC IN ('0', '1000')
                AND CODE_TRX IN ('100', '101')
       ) q
WHERE  rn BETWEEN 2 AND 3
ORDER BY
       DATE_TRX

请注意,SQL Server中需要某种ORDER BY才能使ROW_NUMBER正常工作。