简体   繁体   English

ms sql 2000 row_number代码

[英]ms sql 2000 row_number code

I try write a SQL sintax for MS SQL 2000 with a row_number function... SQL 2000 doesn't support row_number with OVER, so I tried this code.... 我尝试使用row_number函数为MS SQL 2000编写SQL sintax ... SQL 2000不支持带有OVER的row_number,所以我尝试了这段代码....

WHen I wrote this: 我写这篇文章的时候:

    SELECT P1.*,
            (SELECT COUNT(*) FROM Persons P2 
                   WHERE P2.Value<= P1.Value ) AS NewValue
             FROM Persons P1
    WHERE ...

everything is OK, a I get a new column 'NewValue' with row_numbers... 一切都很好,我得到一个新的列'NewValue'与row_numbers ...

But, when I try to update one column in table with this new column, I always get an error: 但是,当我尝试用这个新列更新表中的一列时,我总是收到一个错误:

"Derived table is not updatable because a column of the derived table is derived or constant..."???? “派生表不可更新,因为派生表的列是派生的或常量的......”???? What is wrong?? 怎么了??

Here is a complete sintax: 这是一个完整的sintax:

    UPDATE t
    SET    t.Value= t.NewValue
    FROM   (SELECT P1.*,
            (SELECT COUNT(*) FROM Persons P2 
                WHERE P2.Value<= P1.Value) AS NewValue
             FROM Persons P1) t
    WHERE     ....

'Value' is a column in table which I can't update with a values from 'NewValue' column... “值”是表格中的一列,我无法使用“NewValue”列中的值进行更新...

Thank you very much!!! 非常感谢你!!! :) :)

It means that SQL Server is unable to determine how to update the actual data. 这意味着SQL Server无法确定如何更新实际数据。 This error may appear in two cases: 在两种情况下可能会出现此错误:

  1. You're trying to update a constant field. 您正在尝试更新常量字段。 Example: 例:

     update T set Title = N'New title goes here' from (select 'Old title' as Title) as T 
  2. You're trying to update a derived value. 您正在尝试更新派生值。 Example: 例:

     update T set MaxPrice = 512 from (select max(Price) as MaxPrice) as T 

In order to avoid this issue, you may consider adding a primary key to your table, or base your update on an unique index. 为避免此问题,您可以考虑向表中添加主键,或将更新基于唯一索引。 There are few cases where you would need a table with no primary key or an unique index. 在极少数情况下,您需要一个没有主键或唯一索引的表。 If you're completely sure that any primary key or unique index will harm the schema, you may want to simulate the row_number , for example like this : 如果你完全相信任何主键或唯一索引会损害架构,您可能要模拟的row_number ,例如像这样

select 
    RowNumber = identity(int, 1, 1),
    c.LastName,
    c.FirstName
into #Customer_RowID
from SalesLT.Customer c
order by c.LastName asc

Given the lack of unique constraint, make sure you do the select-update within a transaction to avoid updating a different row. 鉴于缺少唯一约束,请确保在事务中执行select-update以避免更新其他行。

You should shift things around and do your counting directly as a subquery in the SET clause: 您应该将事物转移并直接作为SET子句中的子查询进行计数:

UPDATE P1
SET    Value= (SELECT COUNT(*) FROM Persons P2 
            WHERE P2.Value<= P1.Value)
FROM   Persons P1
WHERE     ....

I am not sure why this syntax doesn't work in SQL Server 2000. I'm pretty sure the syntax would work in more recent versions of SQL Server. 我不确定为什么这种语法在SQL Server 2000中不起作用。我很确定语法可以在更新版本的SQL Server中使用。

Perhaps you should consider upgrading to a supported product. 也许您应该考虑升级到支持的产品。

This version might work: 此版本可能有效:

   UPDATE Persons
     SET t.Value =  (SELECT COUNT(*) FROM Persons P2 WHERE P2.Value <= Persons.Value)
     WHERE     ....;

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

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