February 28, 2012 at 10:05 am
I have two tables: table1 and table2
I have following columns in table1: id,name
I have following columns in table2:p_key,desc
I need to update table1 based on condition from both table1 and table2.
How should i do this?
for example I wanted to do:
update table1 set id=1 where table2.desc='solution'
but this update statement is giving me error?
what should be correct statement?
February 28, 2012 at 10:09 am
You can have join clause on an update statement. BOL (Books On Line) has an article about it with example. Look for the article that is called "Changing Data by Using the FROM Clause"
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
March 2, 2012 at 2:16 am
Syntax is almost the same as for SELECT.
So, if you have a select:
--update t1 set t1.id=t2.somecolumn
--select *
from table1 t1
join table2 t2 on t1.id=t2.id
where t2.desc='solution'
Just uncomment the line with "select", and if you are pleased with result, uncomment the line with "UPDATE"
(only one line should be uncommented at a time).
Hope that helps,
Vedran
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply