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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Correlated Update

Author  Topic 

Cyclonik
Posting Yak Master

114 Posts

Posted - 2006-01-26 : 14:38:46
What is the correct syntax for a correlated update in SQL Server?

Here is the Oracle syntax which does not work in SQL Server 2k:

UPDATE table1 t_alias1
SET column = (
SELECT expr
FROM table2 t_alias2
WHERE t_alias1.column = t_alias2.column);

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-01-26 : 14:45:37
UPDATE table1
SET t_alias1.column = t_alias2.expr
FROM table1 t_alias1, table2 t_alias2
WHERE t_alias1.column = t_alias2.column
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-26 : 19:56:28
Or use the table alias name in the UPDATE
UPDATE t_alias1
SET t_alias1.column = t_alias2.expr
FROM table1 t_alias1, table2 t_alias2
WHERE t_alias1.column = t_alias2.column


----------------------------------
'KH'


Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-01-26 : 20:56:05
I prefer using INNER JOIN syntax:


update table1
set
columna = table2.expr
from
table1
inner join
table2
on table1.column1 = table2.column1




CODO ERGO SUM
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-27 : 01:29:37
Also I prefer using Alias names when you use JOINs

update t1
set
t1.columna = t2.expr
from
table1 t1
inner join
table2 t2
on t1.column1 = t2.column1


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -