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.
| Author |
Topic |
|
sasan_vm
Yak Posting Veteran
51 Posts |
Posted - 2008-04-22 : 04:16:10
|
| Hello,i have a table with million record, an bigint filed like:ID Name-------------------------------------1 Adam4 Bob5 Daivid100 ...Also i have unique index on filed ID, i want update this filed for any row to get row number and to be like:ID Name-------------------------------------1 Adam2 Bob3 Daivid4 ...Can any one show me sample code ?Kind Regards,sasan. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-22 : 04:22:31
|
UPDATE fSET f.ID = f.RecIDFROM (SELECT ID, ROW_NUMBER() OVER (ORDER BY ID) AS RecID FROM Table) AS f E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-22 : 04:37:39
|
If not using SQL 2005 or compatibility level is below 90 use:-UPDATE fSET f.ID = f.RecIDFROM (SELECT t.ID, (SELECT Count(*) + 1 FROM Table WHERE ID <t.ID) AS RecID FROM Table t) AS f |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-22 : 04:52:57
|
And for 1 million records, how long will that update take? Your update is a "triangular join", and involves (1M * 1M / 2) records, which is 500,000,000,000 (500 billion records). E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-22 : 04:56:10
|
If not using SQL Server 2005, you should use thisSELECT ID AS ID_old, IDENTITY(INT, 1, 1) AS ID_newINTO #TempFROM OneMillionRecordTableORDER BY IDUPDATE omrtSET omrt.ID = t.ID_newFROM OneMillionRecordTable AS omrtINNER JOIN #Temp AS t ON t.ID_old = omrt.IDDROP TABLE #Temp E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-04-22 : 05:33:11
|
| But serial number really matters only if you want to show in the front end application MadhivananFailing to plan is Planning to fail |
 |
|
|
sasan_vm
Yak Posting Veteran
51 Posts |
Posted - 2008-04-22 : 07:20:09
|
quote: Originally posted by Peso If not using SQL Server 2005, you should use thisSELECT ID AS ID_old, IDENTITY(INT, 1, 1) AS ID_newINTO #TempFROM OneMillionRecordTableORDER BY IDUPDATE omrtSET omrt.ID = t.ID_newFROM OneMillionRecordTable AS omrtINNER JOIN #Temp AS t ON t.ID_old = omrt.IDDROP TABLE #Temp E 12°55'05.25"N 56°04'39.16"
Thank you |
 |
|
|
|
|
|
|
|