Eliminating ROW_NUMBER() for SQL 2000

霸气de小男生 提交于 2019-12-10 22:21:38

问题


I have to migrate a sql to work on Microsoft SQL Server 2000. Unfortunately the current sql uses the function ROW_NUMBER() which is not yet supported in this version. Therefore I have to find something similar.

Below my SQL (I used * instead of listing all columns)

SELECT [Id], ROW_NUMBER() OVER (ORDER BY InstallmentNumber, ID ASC) AS ROWID
FROM [ARAS].[ARAS].[Movement]

回答1:


Using a temp table with an identity column to simulate the ROW_NUMBER may be your best bet performance wise:

CREATE TABLE #tmpRowNum (
    ROWID INT IDENTITY(1,1),
    ID INT
)

INSERT INTO #tmpRowNum
    (ID)
    SELECT ID
        FROM [ARAS].[ARAS].[Movement]
        ORDER BY InstallmentNumber, ID



回答2:


Not too sure, but here's a starting-point:

SELECT [Id],
  ( SELECT SUM(1)
    FROM [ARAS].[ARAS].[Movement]
    WHERE InstallmentNumber <= OuterMovement.InstallmentNumber
      AND ID <= OuterMovement.ID
  ) AS ‘Row Number’
FROM [ARAS].[ARAS].[Movement] AS OuterMovement



回答3:


You can make a variable table giving the "ROWID" an INT Identity value and inserting the values into that.

You can also do while w/o using the ROWID. The performance wouldn't be great though...

DECLARE @id INT, @SUM INT
SELECT @id = MIN([Id]) FROM [ARAS].[ARAS].[Movement]    


WHILE EXISTS (SELECT [Id] FROM [ARAS].[ARAS].[Movement] WHERE [Id] >= @id)
BEGIN
    ... do something with the ID ..
    SELECT @SUM = SUM(...) FROM [ARAS].[ARAS].[Movement] WHERE [Id] > @id

    SELECT @id = MIN([Id]) FROM [ARAS].[ARAS].[Movement] WHERE [Id] > @id
END


来源:https://stackoverflow.com/questions/5705855/eliminating-row-number-for-sql-2000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!