Thursday, August 13, 2009

Multiple Update in SQL 2008

Problem:
1) Table AAA with null ID and other info
2) Table BBB with ID and other info
3) get the matching data with ID from table BBB to AAA

Solution
1) SQL cursor approch -- I'd avoid
2) SQL Temp table approch -- good & quick

Here is the SQL script for (2) approch:

(1)
create table #tempdata (
firstname varchar(50),
lastname varchar(50),
dob datetime,
id uniqueidentifier
)

insert into #tempdata
select b.FirstName,b.LastName,b.BirthDate, b.id
from
AAA a,BBB b
where
a.FirstName = b.FirstName
and a.LastName = b.LastName
and a.DOB = b.birthDate

select * from #tempdata

--update statement

update AAA
set id = #tempdata.id from #tempdata where
AAA.FirstName = #tempdata.FirstName
and AAA.LastName = #tempdata.LastName
and AAA.DOB = #tempdata.dob


enjoy

No comments: