views:

31

answers:

1

Hi,

I have table in my sql server database which I want to convert to PK column

To do that I want to change value of each row in this column to 1,2,3 ...

Could You write T-Sql query for that task ?

Thanks for help

begin state:

Id | Name |
----------
1  | One  |
2  | Two  |
2  | Three|
x  | xxx  |

result:

Id | Name |
----------
1  | One  |
2  | Two  |
3  | Three|
4  | xxx  |
+2  A: 
;with cte as
(
SELECT Id, ROW_NUMBER() over (order by Id) as rn
from YourTable
)
UPDATE cte SET Id = rn
Martin Smith
Good, but might not be appropriate if there are FK relationships to the id column.
spender
@spender - There can't be. They would need a unique constraint.
Martin Smith