I would like to know if there is a way to use an order by clause when updating a table. I am updating a table and setting a consecutive number, that's why the order of the update is important. Using the following sql statement, I was able to solve it without using a cursor:
DECLARE @Number INT = 0
UPDATE Test
SET @Number = Number = @Number +1
now what I'd like to to do is an order by clause like so:
DECLARE @Number INT = 0
UPDATE Test
SET @Number = Number = @Number +1
ORDER BY Test.Id DESC
I've read: How to update and order by using ms sql The solutions to this question do not solve the ordering problem - they just filter the items on which the update is applied.
9条答案
按热度按时间goqiplq21#
No.
Not a documented 100% supported way. There is an approach sometimes used for calculating running totals called " quirky update " that suggests that it might update in order of clustered index if certain conditions are met but as far as I know this relies completely on empirical observation rather than any guarantee.
But what version of SQL Server are you on? If SQL2005+ you might be able to do something with
row_number
and a CTE (You can update the CTE)n6lpvg4x2#
You can not use ORDER BY as part of the UPDATE statement (you can use in sub-selects that are part of the update).
q9yhzks03#
Edit
Following solution could have problems with clustered indexes involved as mentioned here . Thanks to Martin for pointing this out.
The answer is kept to educate those (like me) who don't know all side-effects or ins and outs of SQL Server.
Expanding on the answer gaven by Quassnoi in your link, following works
pw9qyyiw4#
The row_number() function would be the best approach to this problem.
2lpgd9685#
update based on Ordering by the order of values in a SQL IN() clause
Solution:
This updates based on descending 3,2,1
Hope helps someone.
gxwragnw6#
I had a similar problem and solved it using ROW_NUMBER() in combination with the OVER keyword. The task was to retrospectively populate a new TicketNo (integer) field in a simple table based on the original CreatedDate, and grouped by ModuleId - so that ticket numbers started at 1 within each Module group and incremented by date. The table already had a TicketID primary key (a GUID).
Here's the SQL:
Worked a treat!
kwvwclae7#
I ran into the same problem and was able to resolve it in very powerful way that allows unlimited sorting possibilities.
I created a View using (saving) 2 sort orders (*explanation on how to do so below).
After that I simply applied the update queries to the View created and it worked great.
Here are the 2 queries I used on the view:
1st Query:
2nd Query:
*To be able to save the sorting on the View I put TOP into the SELECT statement. This very useful workaround allows the View results to be returned sorted as set when the View was created when the View is opened. In my case it looked like:
(NOTE: Using this workaround will place an big load on the server if using a large table and it is therefore recommended to include as few fields as possible in the view if working with large tables)
Running: SQL Server 2005 on a Windows Server 2003
Additional Keywords: How to Update a SQL column with Ascending or Descending Numbers - Numeric Values / how to set order in SQL update statement / how to save order by in sql view / increment sql update / auto autoincrement sql update / create sql field with ascending numbers
t1qtbnec8#
In the above example query simply update the student Roll_No column depending on the student Frist_Name column. From 1 to No_of_records in the table. I hope it's clear now.
pqwbnv8z9#