Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hello,I have a problem with UPDATE clause. I have a list of users and now I would like to make them an unique number per user. If I use UPDATE clause, what should I do if I want to start the numbers of the column from 1 (row 1) and the next (row 2) would be 1 greater than the previous. I mean 1,2,3, .... Thanks!
rohitvishwakarma
Posting Yak Master
232 Posts
Posted - 2010-09-22 : 04:28:29
you mean to say:in table tbl_xyz you are having users like:[user]user1user2user3user4user5user6and after UPDATE ?? you want something like this[user] [uniqueId]user1 1user2 2user3 3user4 4user5 5....Is it so?
Kristen
Test
22859 Posts
Posted - 2010-09-22 : 04:29:50
Can you add an column to the table with IDENTITY property? That will automatically assign a unique ID to all existing rows (and to any new rows that are inserted in the future)
Kristen
Test
22859 Posts
Posted - 2010-09-22 : 04:31:29
If you want to just assign a number to existing rows here's what I would do
SELECT [T_ID] = IDENTITY(int, 1, 1), [T_MyPK] = MyPKINTO #TEMPFROM MyTableORDER BY ... columns controlling ID ordering ...UPDATE USET MyID_Column = T_IDFROM MyTable AS U JOIN #TEMP ON T_MyPK = MyPK
TorspeR
Starting Member
11 Posts
Posted - 2010-09-22 : 04:31:41
Yes, thats right! So I guess there is a way to make this with UPDATE after I've made the uniqueid column?