问题
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