SQL Server 2000 上的 SQL Server ROW_NUMBER()?

发布于 2024-09-30 00:38:33 字数 585 浏览 4 评论 0原文

我有一个查询,允许我通过给定最小和最大限制来从数据库表中获取记录。

它是这样的:

  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;

现在,它在 SQL Server 2005 和 SQL Server 2008 上运行得像天堂一样,但尝试在 SQL Server 2000 数据库上运行它并显示:

ROW_NUMBER 这是一个未知的函数名称或类似名称。

我能做些什么??

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:

ROW_NUMBER it's an unknown function name or something like that.

What can I do??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

耳钉梦 2024-10-07 00:38:33
  • 有一个带有自连接解决方​​案的 COUNT(*) 这里会严重扩展
  • 您可以加载带有 IDENTITY 列的临时表并读回,但不能保证它可以工作(找不到关于它的文章,在 MS 研讨会上被告知)之前)

这两种解决方案都不支持 PARTITION BY

我没有提到基于循环或 CURSOR 的解决方案,这些解决方案可能更糟糕

编辑 20011 年 5 月 20 日

为什么 IDENTITY 不起作用的示例演示:
插入的记录是否始终接收连续的标识值

  • There is a COUNT(*) with SELF JOIN solution here that will scale badly
  • 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)

Neither solution will support PARTITION BY

I've not mentioned loop or CURSOR based solutions which are probably worse

Edit 20 May 20011

Example demo of why IDENTITY won't work:
Do Inserted Records Always Receive Contiguous Identity Values

我们的影子 2024-10-07 00:38:33

我知道这个线程有点旧,但对于寻找相同解决方案的其他人来说,我认为知道这个问题有一个好的解决方案将会很有用。

请参阅此处的原始链接

对于那些不想点击链接,我复制并粘贴了下面的代码。再次强调,功劳归于原始发布者

以下是 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

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

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.

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:

SELECT * FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY PartitionByColumn 
    ORDER BY OrderByColumn DESC) AS rowNumber FROM MyTable) AS rw1 
  WHERE rw1.rowNumber = 1
亢潮 2024-10-07 00:38:33

使用其他功能或升级数据库。 ROW_NUMBER 在 2000 版本的数据库中并不存在。观点。你对此无能为力。

Use another function or upgrade your database. ROW_NUMBER did not exist back in the 2000 version of the database. Point. Nothing you can do about it.

も星光 2024-10-07 00:38:33

这是我对问题的解决方案:

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

说明:

  1. 创建一个内存表
  2. 插入数据(您的查询),行号为0,使用
  3. int 变量更新行号字段,该变量在下一条记录的同一更新中递增(实际上是变量先递增后更新,所以会从1)
  4. 从内存表中“选择”结果开始。

你可能会问,为什么我不在 select 语句中使用变量呢?会更简单但是不允许,除非没有结果。在更新中做到这一点就可以了。

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
  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)
  4. "select" the result from the memory table.

You may ask, why don't i use the variable in the select statement? It would be simpler but it's not allowed, only if there is no result. It's ok to do it in an update.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文