How to put row number for sql query in sql 2000 where rownumber() is not supporting?
views:
232answers:
6
A:
You can't use Row_Number() in Sql Server 2000 - it was introduced in 2005.
In case you wanted to use Row_Number for paging, here are some ideas on how to perform efficient paging in Sql 2000:
Yaakov Ellis
2009-11-16 13:07:16
+4
A:
You can always try to use a temp table with an identity column
DECLARE @table TABLE(
[id] INT IDENTITY(1,1),
Val VARCHAR(10)
)
DECLARE @TableFrom TABLE(
Val VARCHAR(10)
)
INSERT INTO @TableFrom (Val) SELECT 'A'
INSERT INTO @TableFrom (Val) SELECT 'B'
INSERT INTO @TableFrom (Val) SELECT 'C'
INSERT INTO @TableFrom (Val) SELECT 'D'
INSERT INTO @table (Val) SELECT * FROM @TableFrom ORDER BY Val DESC
SELECT * FROM @table
Some of the best paging i have seen in Sql Server 2000 uses this pattern
DECLARE @PageStart INT,
@PageEnd INT
SELECT @PageStart = 51,
@PageEnd = 100
SELECT <TABLE>.*
FROM (
SELECT TOP (@PageStart - 1)
<ID>
FROM (
SELECT TOP (@PageEnd)
<ID>
FROM TABLE
ORDER BY <ID> ASC
) SUB
ORDER BY SUB.<ID> DESC
) SUB INNER JOIN
<TABLE> ON SUB.<ID> = <TABLE>.<ID>
ORDER BY SUB.<ID>
astander
2009-11-16 13:10:59
Except, this is not a temp table but a table variable, and they are not support in 200 either
Andomar
2009-11-16 13:12:17
Have a look at this http://support.microsoft.com/kb/305977
astander
2009-11-16 13:14:04
+1 nice. Here's my upvote then!
Andomar
2009-11-16 13:16:39
Table variables are supported in SQL Server 2000
HLGEM
2009-11-16 14:28:01
A:
Could you elaborate how the below query will solve the problem?
SELECT ( SELECT SUM(1)
FROM specimen_source_ref
WHERE specimen_source_rcd <= reg.specimen_source_rcd
) AS 'Row Number'
,*
FROM specimen_source_ref reg
This will give you the count of the number of items smaller or equal to the current item in the list, but will not give you the unique row_number, if there are duplcate items
astander
2009-11-16 13:22:57
A:
Another way to create a temp table with an identity to use:
SELECT Field1, Field2, IDENTITY(int, 1,1) AS MyID
INTO #Temp
FROM Table1
HLGEM
2009-11-16 14:31:14
A:
SELECT Field1, Field2, IDENTITY(int, 1,1) AS MyID
INTO #Temp
FROM Table1
too good :)
NSQl
2010-07-22 06:28:12