简体   繁体   English

SQL Server 2000上的SQL Server ROW_NUMBER()?

[英]SQL Server ROW_NUMBER() on SQL Server 2000?

I have a query that allows me to get records from a database table by giving it a minimum and maximum limit. 我有一个查询,允许我通过给它一个最小和最大限制从数据库表中获取记录。

It goes like this: 它是这样的:

  SELECT T1.CDUSUARIO, T1.DSALIAS, T1.DSNOMBRE_EMPRESA, T1.DSCARGO, T1.DSDIRECCION_CORREO, T1.CDUSUARIO_ADMINISTRADOR, T1.FEMODIFICACION 
    FROM (SELECT *, 
               ROW_NUMBER() OVER (ORDER BY CDUSUARIO) as row FROM TBL_USUARIOS ) as T1 
   WHERE row > @limiteInf 
     and row <= @limiteSup 
ORDER BY DSALIAS ASC;

Now, it works like heaven on SQL Server 2005 and SQL Server 2008 but tried to run it on an SQL Server 2000 database and says: 现在,它在SQL Server 2005和SQL Server 2008上就像天堂一样,但是试图在SQL Server 2000数据库上运行它并说:

ROW_NUMBER it's an unknown function name or something like that. ROW_NUMBER它是一个未知的函数名称或类似的东西。

What can I do?? 我能做什么??

  • There is a COUNT(*) with SELF JOIN solution here that will scale badly 有一个COUNT(*)与自连接解决方案在这里 ,将扩展严重
  • You can load a temp table with an IDENTITY column and read back but it's not guaranteed to work (can't find article on it, was told at an MS Seminar years ago) 您可以加载带有IDENTITY列的临时表并回读但不保证可以正常工作(找不到文章,多年前曾在MS研讨会上讲过)

Neither solution will support PARTITION BY 这两种解决方案都不支持PARTITION BY

I've not mentioned loop or CURSOR based solutions which are probably worse 我没有提到基于循环或基于CURSOR的解决方案可能更糟

Edit 20 May 20011 编辑20011年5月20日

Example demo of why IDENTITY won't work: IDENTITY无法工作的示例演示:
Do Inserted Records Always Receive Contiguous Identity Values 插入的记录是否始终接收连续的标识值

I know this thread is bit old, but for anyone else looking for same solution, I think it will be useful to know that there is a good solution for this problem. 我知道这个线程有点旧,但对于其他寻找相同解决方案的人来说,我认为知道这个问题有一个很好的解决方案会很有用。

Please see the original link here 请在此处查看原始链接

For those who do not want to click on the link, I have copied and pasted the code below. 对于那些不想点击链接的人,我复制并粘贴了以下代码。 Again, credit goes to original publisher 再次,归功于原始出版商

Here is the below SQL for SQL Server 2000 to select the latest version of a record grouping by a single column. 下面是SQL Server 2000的SQL,用于通过单个列选择最新版本的记录分组。

SELECT *
  FROM (
    SELECT *, (
      SELECT COUNT(*)
        FROM MyTable AS counter
      WHERE counter.PartitionByColumn = MyTable.PartitionByColumn
        AND  counter.OrderByColumn >= MyTable.OrderByColumn
      ) AS rowNumber
    FROM MyTable
  ) AS r1
  WHERE r1.rowNumber = 1

Same code in SQL Server 2005 would look like this: SQL Server 2005中的相同代码如下所示:

SELECT * FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY PartitionByColumn 
    ORDER BY OrderByColumn DESC) AS rowNumber FROM MyTable) AS rw1 
  WHERE rw1.rowNumber = 1

Use another function or upgrade your database. 使用其他功能或升级数据库。 ROW_NUMBER did not exist back in the 2000 version of the database. ROW_NUMBER在2000版本的数据库中不存在。 Point. 点。 Nothing you can do about it. 你无能为力。

This is my solution to the problem: 这是我解决问题的方法:

declare @i int
declare @t table (row int, stuff varchar(99))
insert into @t
select 0,stuff from mytable -- <= your query
set @i=0
update @t set row=@i, @i=@i+1
select * from @t

Explanation: 说明:

  1. create a memory table 创建一个内存表
  2. insert data (your query) with the row number as 0 插入数据(您的查询),行号为0
  3. update the row number field with an int variable which is incremented in the same update for the next record (actually the variable is incremented first and then updated, so it will start from 1) 使用int变量更新行号字段,该变量在下一条记录的相同更新中递增(实际上变量先递增然后更新,因此它将从1开始)
  4. "select" the result from the memory table. “选择”内存表中的结果。

You may ask, why don't i use the variable in the select statement? 你可能会问,为什么我不在select语句中使用变量? It would be simpler but it's not allowed, only if there is no result. 它会更简单,但只有在没有结果的情况下才允许。 It's ok to do it in an update. 可以在更新中执行此操作。

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

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