views:

232

answers:

6

How to put row number for sql query in sql 2000 where rownumber() is not supporting?

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
+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
Except, this is not a temp table but a table variable, and they are not support in 200 either
Andomar
Have a look at this http://support.microsoft.com/kb/305977
astander
+1 nice. Here's my upvote then!
Andomar
Table variables are supported in SQL Server 2000
HLGEM
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
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
A: 

SELECT Field1, Field2, IDENTITY(int, 1,1) AS MyID
INTO #Temp
FROM Table1

too good :)

NSQl