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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need Help UPDATE stmt

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 script


create table temp_tbl
(id int,
name varchar(10)
)
insert into temp_tbl
select 1,'Ram'
union all
select null,'Sam'
union all
select null,'Sita'

UPDATE a
SET a.id =
(SELECT MAX(id)+1 FROM temp_tbl b
WHERE 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 @Sample
SELECT 1, 'Ram' UNION ALL
SELECT 3, 'Kam' UNION ALL
SELECT 4, 'Rita' UNION ALL
SELECT NULL, 'Sam' UNION ALL
SELECT NULL, 'Sita'

SELECT *
FROM @Sample

UPDATE s
SET s.ID = f.ID
FROM @Sample AS s
INNER 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.Name

SELECT *
FROM @Sample[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

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 ?
Go to Top of Page

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		s
SET s.ID = f.ID
FROM {Your table name here} AS s
INNER 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"
Go to Top of Page

frank.svs
Constraint Violating Yak Guru

368 Posts

Posted - 2009-02-13 : 06:25:18
Thanks for the Help!
Go to Top of Page

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 script


create table temp_tbl
(id int,
name varchar(10)
)
insert into temp_tbl
select 1,'Ram'
union all
select null,'Sam'
union all
select null,'Sita'

UPDATE a
SET a.id =
(SELECT MAX(id)+1 FROM temp_tbl b
WHERE 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?
Go to Top of Page
   

- Advertisement -