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 |
aswindba1
Yak Posting Veteran
62 Posts |
Posted - 2014-04-23 : 22:44:32
|
I want to update table2 by checking table1.if trauma has atleast 1 then clm2 in table2 would be 1if infec has atleast 1 then clm2 in table2 would be 2table1Clm1 Clm2Trauma 1Trauma 1Trauma 1Infec 2Infec 2Trauma Nulltable2clm1 clm2Trauma 1infec 2I have to update table2 based on table one ny checking multiple columns.Please help me. |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2014-04-23 : 23:26:36
|
[code]declare @table1 table(Clm1 varchar(30), Clm2 int)insert into @table1select 'Trauma',1union all select 'Trauma',1union all select 'Trauma',1union all select 'Infec',2union all select 'Infec',2union all select 'Trauma',Nulldeclare @table2 table(clm1 varchar(30), clm2 int)insert into @table2select 'Trauma',0union all select 'infec', 0update aset a.clm2 = case when a.clm1 = 'Trauma' then 1 when a.clm1 = 'Infec' then 2 endfrom @table2 ajoin (select clm1 from @table1group by clm1having count(1) >= 1)bon a.clm1 = b.Clm1select * from @table2[/code] |
|
|
WAmin
Starting Member
16 Posts |
Posted - 2014-04-23 : 23:27:09
|
IF EXISTS (SELECT * FROM TABLE1 WHERE Trauma=1) OR EXISTS (SELECT * FROM TABLE1 WHERE infec=1)UPDATE TABLE2 SET clm2=CASE WHEN EXISTS (SELECT * FROM TABLE1 WHERE Trauma=1) THEN 1 ELSE CASE WHEN EXISTS (SELECT * FROM TABLE1 WHERE infec=1) THEN 2 ELSE clm2 END ENDI don't know what are your priorities? Do Trauma should take precedence or infec?BTW your whole question seems like a riddle, i am not an expert but it is good practice to make a sample table and sample insert to show others what you really want. |
|
|
|
|
|