I have a table where the following data is available:
ProductID ProductName Pref
--------- ----------- ----
001 ABC 1
005 CDE 3
009 EFG 2
008 HIJ 4
003 IJK 5
I would like to update [Pref] of [ProductID] with “005” fromm 3 to 5. It is easy by using UPDATE SQL command. But [Pref] column is unique and I can not do such SQL command to update.
What I did: Sorting [Pref]
ProductID ProductName Pref
--------- ----------- ----
001 ABC 1
009 EFG 2
005 CDE 3 <---
008 HIJ 4
003 IJK 5
What I want to see my data as follows:
ProductID ProductName Pref
--------- - ---------- ----
001 ABC 1 -->1
009 EFG 2 -->2
008 HIJ 4 -->3
003 IJK 5 -->4
005 CDE 3 -->5<--
What should be the SQL command to do such change.
I am using C# .NET (IDE:Visual Studio 2005) and MS SQL SERVER 2005.
Thank you so much for your help.
You could do it like this:
With a bit of parameterization and searching for the highest current number, this could become quite generic.
Edit (see comments):