how to write this sql

I have table T1 with fields T1.ID, T1.CheckBoxCol

The T2 tabel has the same ID as T2.ID and T2.CheckBox1, T2.CheckBox2, T2. CheckBox3

Now, I need to check T1 and
if T1.CheckBoxCol=1 then set T2.CheckBox1=1
else if T1.CheckBoxCol=2 then set T2.CheckBox2=1
else if T1.CheckBoxCol=3 then set T2.CheckBox3=1

for each T1.ID

How can I do this?

[384 byte] By [JIM.H.] at [2007-12-22]
# 1

create Table T1 (ID int, C1 int)
create Table T2 (ID int, C1 int,c2 int,c3 int)

insert into T1 select 1 ,1
insert into T1 select 2 ,2
insert into T1 select 3 ,3

insert into T2 select 1 ,0,0,0
insert into T2 select 2 , 0,0,0
insert into T2 select 3 ,0, 0,0


Update T2 set c1= (select case when isnull((select C1 from T1 where T1.id=T2.id ),-1)=1 then 1 else 0 end), c2=(select case when isnull((select C1 from T1 where T1.id=T2.id ),-1)=2 then 1 else 0 end), c3=(select case when isnull((select C1 from T1 where T1.id=T2.id ),-1)=3 then 1 else 0 end)

regards

mobin

mobin.p at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2

Hello,

try the following syntax....

update Test2

set CheckBoxCol1 =

Case T1.TID

when 1 then 1

when 2 then 1

when 3 then 1

when 4 then 1

else 0

end,

CheckBoxCol2 =

Case T1.TID

when 1 then 1

when 2 then 1

when 3 then 1

when 4 then 1

else 0

end,

CheckBoxCol3 =

Case T1.TID

when 1 then 1

when 2 then 1

when 3 then 1

when 4 then 1

else 0

end

from Test1 T1 inner join Test2 T2

ON T1.TID = T2.TID

Thanks....

asiaindian at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 3
UPDATE T2
SET CHECKBOX1 = (CASE WHEN T1.CHECKBOXCOL = 1 THEN 1 ELSE 0 END)
, CHECKBOX2 = (CASE WHEN T1.CHECKBOXCOL = 2 THEN 1 ELSE 0 END)
, CHECKBOX3 = (CASE WHEN T1.CHECKBOXCOL = 3 THEN 1 ELSE 0 END)
FROM T2
JOIN T1
ON T1.ID = T2.ID
cheung at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified