tags:

views:

213

answers:

7
SELECT t1.status, t3.guid, t3.objectID
FROM Table1 t1, Table2 t2, Table3 t3
WHERE t2.ID = t3.ID
   AND t1.ID = t2.ID
   AND t3.Guid IN ('', '', '')

How can I convert this to an update statement where I set the t1.status = 1?

+1  A: 

Try something like this:

update Table1
set status = 1
from Table1 t1
    inner join Table2 t2 on t1.ID = t2.ID
    inner join Table3 t3 on t2.ID = t3.ID
where t3.Guid IN ('', '', '');
Andrew Hare
+1  A: 
UPDATE Table1
SET status = 1
FROM Table1 t1, Table2 t2, Table3 t3
WHERE t2.ID = t3.ID
   AND t1.ID = t2.ID
   AND t3.Guid IN ('', '', '')
bdukes
A: 
update Table1
set status = 1
FROM Table1 t1, Table2 t2, Table3 t3
WHERE t2.ID = t3.ID
   AND t1.ID = t2.ID
   AND t3.Guid IN ('', '', '')
TrickyNixon
A: 
UPDATE Table1 set status = 1 
from table2 t2, table 3 t3 
where t2.ID = t3.ID 
and Table1.ID = t2.ID 
and t3.GUID in ('','','')
Ryan
A: 

I believe in MS SQL Server (but not Oracle), you can write:

UPDATE TABLE1 
set t1.status = 1
FROM Table1 t1, Table2 t2, Table3 t3
WHERE t2.ID = t3.ID
   AND t1.ID = t2.ID
   AND t3.Guid IN ('', '', '')
James Curran
A: 
MERGE
INTO Table1 t1
USING
  (
  SELECT t2.id
  FROM Table2 t2, Table3 t3
  WHERE 
    t3.ID = t2.id
    AND t3.GUID IN ('', '', '')
 ) to
ON t1.id = to.id
WHEN MATCHED THEN
  UPDATE
  SET t1.status = 1
Quassnoi
+1  A: 

I would start by converting it to using joins instead of "classical joining":

select t1.status, t3.guid, t3.objectID
from Table1 t1
inner join Table2 t2 on t2.ID = t1.ID
inner join Table3 t3 on t3.ID = t2.ID
where t3.Guid in ('', '', '')

Then you can just rip of the select statement and slap an update and set statement on it:

update t1
set status = 1
from Table1 t1
inner join Table2 t2 on t2.ID = t1.ID
inner join Table3 t3 on t3.ID = t2.ID
where t3.Guid in ('', '', '')
Guffa
+1 for explaining thought process.
sfossen