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 2008 Forums
 Transact-SQL (2008)
 conditional updates in merge

Author  Topic 

nidabp
Starting Member

15 Posts

Posted - 2011-04-29 : 06:08:41
Hi,

Merge TableA as A
using(
(SELECT query)) as B
ON A.x = B.x
When Matched and ReportPeriod = 1 Then Update
Set JanMet = 1 and JanMetALT = 1
When Matched and ReportPeriod = 2 Then Update
Set FebMet = 1 and FebMetAlt = 1
When Matched and ReportPeriod = 3 Then Update
Set MarMet = 1 and FebMetAlt = 1
...

Is there a way to have conditional updates and inserts to different columns

Thanx

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-29 : 08:18:29
When Matched then update
set
JanMet = case when ReportPeriod = 1 then 1 else JanMet end ,
JanMetALT = case when ReportPeriod = 1 then 1 else JanMetALT end ,
FebMet = case when ReportPeriod = 2 then 1 else FebMet end ,
FebMetALT = case when ReportPeriod = 2 then 1 else FebMetALT end ,
...

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

nidabp
Starting Member

15 Posts

Posted - 2011-05-02 : 04:22:51
Thank you...It fixed the issue..
Go to Top of Page
   

- Advertisement -