Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

How to update Identity Column from ROW_NUMBER in sql server 2008?

I am using this query for update but it's getting error:-
Cannot update identity column 'SNO'

SQL
set IDENTITY_INSERT tableSalesQuotation ON
update tableSalesQuotation
set
SNO=SubQuery.SNO
from
(SELECT Row_Number() OVER (ORDER BY SNO) as SNO
FROM tableSalesQuotation
) SubQuery
set IDENTITY_INSERT tableSalesQuotation OFF



please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted
Updated 11-Apr-14 1:52am
v4
Comments
Mike Meinz 11-Apr-14 7:20am    
Nobody does this! If you think you need to do this, please re-evaluate your data and application design.

Setting the
SQL
IDENTITY_INSERT
property of the table to ON will not make any difference as this only affects new records and not existing ones.
If you really need to change the values of an identity column, this can be accomplished in 2 steps. The first step is to INSERT new records containing the same information of the existing records whose identity column needs to be updated assigning the new IDs for these new records. The second step is deleting the original records.

but then that's a risk because you may not want to loose your data, and also if there is any referential integrity like Foreign keys to other tables. Then you also need to remove the entries from that tables.
So quite a risk. but if you are doing it in a simple purpose then try this.
SQL
set identity_insert YourTable ON
--Then delete your row and reinsert it with different identity.
--Once you have done the insert don't forget to turn identity_insert off
set identity_insert YourTable OFF
 
Share this answer
 
v2
Comments
[no name] 11-Apr-14 7:42am    
I have change in my query but error is stilling here:-
set IDENTITY_INSERT tableSalesQuotation ON
update tableSalesQuotation
set
SNO=SubQuery.SNO
from
(SELECT Row_Number() OVER (ORDER BY SNO) as SNO
FROM tableSalesQuotation
) SubQuery
set IDENTITY_INSERT tableSalesQuotation OFF
Schatak 11-Apr-14 8:49am    
This is what i have written above you can't do it like this, if you want to take the risk then delete existing record and insert records with Row_number() instead of updating it.
You can't update Identity columns without some careful playing - and it's a very, very bad idea at the best of times!
Think about it: you may well have other tables whose data depends on the value here, (which may or may not be marked as foreign keys, though they should be) and you may have other users accessing the database while you are changing it.

It is a much, much better idea to return a row number instead if you feel you need one, as that will have no gaps and will not cause any data problems.

If you really must do it, then a quick google will show you how - but I strongly advise against it!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900