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 |
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2009-02-13 : 03:46:45
|
| Hi Pals,Small Help Required.Need to update all the values of the ID column by getting the MAX(id).Something like need to generate sequnce numbers by getting the max(id) from the table.Can we implement this using single update.I have tried with the below update but i think NULL values are causing some problem.We can doing this using single UPDATE instead of going to cursors or anyother looping statements.Any help would be greatly appreciated.Thanks!Here is the scriptcreate table temp_tbl(id int, name varchar(10))insert into temp_tbl select 1,'Ram'union allselect null,'Sam'union allselect null,'Sita'UPDATE aSET a.id = (SELECT MAX(id)+1 FROM temp_tbl bWHERE b.id = a.id ) FROM temp_tbl a |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-13 : 04:01:46
|
[code]DECLARE @Sample TABLE ( ID INT, Name VARCHAR(10) ) INSERT @SampleSELECT 1, 'Ram' UNION ALLSELECT 3, 'Kam' UNION ALLSELECT 4, 'Rita' UNION ALLSELECT NULL, 'Sam' UNION ALLSELECT NULL, 'Sita'SELECT *FROM @SampleUPDATE sSET s.ID = f.IDFROM @Sample AS sINNER JOIN ( SELECT g.ID, w.Name FROM ( SELECT d.ID, ROW_NUMBER() OVER (ORDER BY d.ID) AS recID FROM ( SELECT ROW_NUMBER() OVER (ORDER BY ID) AS ID FROM @Sample ) AS d LEFT JOIN @Sample AS s ON s.ID = d.ID WHERE s.ID IS NULL ) AS g INNER JOIN ( SELECT ROW_NUMBER() OVER (ORDER BY Name) AS recID, Name FROM @Sample WHERE ID IS NULL ) AS w ON w.recID = g.recID ) AS f ON f.Name = s.NameSELECT *FROM @Sample[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2009-02-13 : 04:29:11
|
| Thank You Very much!Can we implement the same thing using single UPDATE ? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-13 : 04:36:03
|
What?There is only one update, and as auch it qualifies as a single update.UPDATE sSET s.ID = f.IDFROM {Your table name here} AS sINNER JOIN ( SELECT g.ID, w.Name FROM ( SELECT d.ID, ROW_NUMBER() OVER (ORDER BY d.ID) AS recID FROM ( SELECT ROW_NUMBER() OVER (ORDER BY ID) AS ID FROM {Your table name here} ) AS d LEFT JOIN {Your table name here} AS s ON s.ID = d.ID WHERE s.ID IS NULL ) AS g INNER JOIN ( SELECT ROW_NUMBER() OVER (ORDER BY Name) AS recID, Name FROM {Your table name here} WHERE ID IS NULL ) AS w ON w.recID = g.recID ) AS f ON f.Name = s.Name E 12°55'05.63"N 56°04'39.26" |
 |
|
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2009-02-13 : 06:25:18
|
| Thanks for the Help! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-13 : 09:50:29
|
quote: Originally posted by frank.svs Hi Pals,Small Help Required.Need to update all the values of the ID column by getting the MAX(id).Something like need to generate sequnce numbers by getting the max(id) from the table.Can we implement this using single update.I have tried with the below update but i think NULL values are causing some problem.We can doing this using single UPDATE instead of going to cursors or anyother looping statements.Any help would be greatly appreciated.Thanks!Here is the scriptcreate table temp_tbl(id int, name varchar(10))insert into temp_tbl select 1,'Ram'union allselect null,'Sam'union allselect null,'Sita'UPDATE aSET a.id = (SELECT MAX(id)+1 FROM temp_tbl bWHERE b.id = a.id ) FROM temp_tbl a
but wont this update all records in batch with same id value when you do update on a batch? is nt just enough to make id column identity for this so that value gets autogenerated each time? |
 |
|
|
|
|
|
|
|