Author |
Topic |
rkumar28
Starting Member
49 Posts |
Posted - 2006-02-03 : 17:07:31
|
Hi,Is there a way to do Update using IDENTITY function?I have a table whose ID column I am trying to update is based on order by clause of other column in the same table.My sample table as below: Table1. This is just a sample table, the real table has more than 15 columns. Col1…. .ID ------….----- 1…… null 7…….null 2…….null 17…….null 11…….nullI am trying to update the ID column by a number 1, 2, 3….an so on but the order needs to be in the ascending order of col1 data. I am trying to achieve something like this. Col1…. .ID ------….----- 1……..1 7…….3 2…….2 17…….4 11…….5I tried something like below. I am trying to update the table.Update table1set ID = Identity(int,1,1)from table1 torder by t.col1But get an error saying, Incorrect syntax near the keyword 'IDENTITY'. Is there a way to do Update using IDENTITY function?Will appreciate any advice to achieve this without deleting the data in the table.ThanksRaj |
|
TimS
Posting Yak Master
198 Posts |
Posted - 2006-02-03 : 17:42:15
|
PK1 and PK2 are the Primary Key Columns on the table tablenameSELECT IDENTITY ( int , 1 , 1 ) AS tid, PK1, PK2INTO #tempFROM tablenameORDER BY COL1, COL2UPDATE x SET x.tid = t.tidFROM tablename xJOIN #temp t ON x.PK1 = t.PK1 AND x.PK2 = t.PK2DROP TABLE #temp |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-02-03 : 18:12:20
|
A select into won't always respect the order by clause (might be sevice pack dependent but I wouldn't rely on it).You can insert into temp table with a cluserted index then select into from that - never seen that not work but still not guaranteed.best to just calculate itUpdate table1set ID = (select count(*) from table1 t2 where t2.col1 <= t.col1)from table1 t==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2006-02-04 : 06:29:59
|
declare @id intset @id = 0update t2 set @id = t2.id= @id + 1 from (select top 100 percent col1 from table order by col1) as t1 inner loop join table t2 on t2.col1 = t1.col1 option (force order) |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
rkumar28
Starting Member
49 Posts |
Posted - 2006-02-06 : 15:11:04
|
Hi,Thank You so much for replying to this post. I tried the suggested code below. It works fine. Is there a way we can make it work for null values as well. I do have few nulls in col1. For nulls the @Id is getting update as nulls. I am sorry, I forgot to mention on this in my original post.Any suggestions on how to fix this.quote: Originally posted by mmarovic declare @id intset @id = 0update t2 set @id = t2.id= @id + 1 from (select top 100 percent col1 from table order by col1) as t1 inner loop join table t2 on t2.col1 = t1.col1 option (force order)
ThanksRajRaj |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-02-06 : 15:28:54
|
declare @id intset @id = 0update t2set @id = t2.id= @id + 1from (select top 100 percent col1from tableorder by col1) as t1inner loop join table t2 on t2.col1 = t1.col1 or (t2.col1 is null and t1.col1 is null)option (force order)Not sure this will really give you what you want but give it a go.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2006-02-07 : 05:04:23
|
In that case make sure you have index on col1 (in ascending order):declare @id intset @id = 0update t2 set @id = t2.id= @id + 1 from table t2 (index = idx_table_col1) |
|
|
|