Reputation: 2627
I'm trying write a routine in SQL Server that, when run, would traverse specified tables and, if a specific column contains a value, update another value. In pseudo code:
select * from table1
if column1 = true
{
update table2.column1 with value where table2.column2.value = table1.column2.value
}
Basically, traverse table1
and if the value in a specific column is true, update the value of another table's column1
where that row's column2
matches table1
's column2
Thanks
Upvotes: 0
Views: 3201
Reputation: 3218
Not sure I fully understand your requirement, but if you want update all the values using a single update statement this might do:
update table2 set column1 =
(select column1 from table1 t1
where t1.column2 = table2.column2
)
where exists
(select * from table1 t1
where t1.column2 = table2.column2 and t1.column1 = true
)
It could be written to avoid having the 2nd subselect (the EXISTS clause) but the solution is rather dialect-specific. This one is, I believe, more likely to be accepted in an unknown dialect. (Except for the word "true" ... substitute the value you want, there. )
Upvotes: 0
Reputation: 65147
You don't need the IF
, just use a WHERE
clause:
UPDATE T2
SET t2.Column1 = 'blah'
FROM Table2 t2
INNER JOIN Table1 t1
ON t1.value = t2.value
WHERE t1.column1 = 'True'
Upvotes: 3