0

可能重复:
sqlserver 中的 LIMIT 10..20

我尝试在 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?

4

1 回答 1

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

请注意,您需要某种ORDER BY在 SQL Server 中ROW_NUMBER才能工作。

于 2013-01-10T19:00:00.590 回答