简体   繁体   English

如何获取 SQL Server 2000 查询中的当前行号?

[英]How to get the current row number in an SQL Server 2000 query?

如何使用不支持ROW_NUMBER()函数的 SQL Server 2000 在 SQL 查询中获取行号?

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我在 Sql Server 2000 中看到的一些最好的分页使用了这种模式

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>

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

You can't use Row_Number() in Sql Server 2000 - it was introduced in 2005.您不能在 Sql Server 2000 中使用 Row_Number() - 它是在 2005 年引入的。

In case you wanted to use Row_Number for paging, here are some ideas on how to perform efficient paging in Sql 2000:如果您想使用 Row_Number 进行分页,以下是有关如何在 Sql 2000 中执行高效分页的一些想法:

Another way of doing this without using a SQL defined function could be the following:另一种不使用 SQL 定义函数的方法如下:

SELECT 
(SELECT COUNT(1) + 1 FROM YourTable t2 WHERE t2.Id < t.Id) AS RowNumber
FROM YourTable t

It's a bit tricky, but seems simpler that the options that others gave you.这有点棘手,但似乎比其他人给你的选择更简单。

Could you elaborate how the below query will solve the problem?您能否详细说明以下查询将如何解决问题?

SELECT ( SELECT SUM(1)选择(选择总和(1)

FROM specimen_source_ref来自样本来源_参考

WHERE specimen_source_rcd <= reg.specimen_source_rcd哪里标本_源_rcd <= reg.specimen_source_rcd

) AS 'Row Number' ) AS '行号'

,* ,*

FROM specimen_source_ref reg来自样本源引用 reg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM