HeadScratcher (ma***@servicemg.com) writes:
I am trying to speed up my update statements by removing inner select
statements.
Example:
update orders set shipname = (select contactName from
customers where customerid = orders.customerID)
I read some articles which said that I should be able to use an inner
join on the update statement like the following:
update orders set shipname = (select contactName from customers where
customerid = orders.customerID)
But every time that I run this statement I get the follwing error:
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'inner'.
Apparently there was some glitch in the editing. Anyway, this is what
you want:
UPDATE Orders
SET ShipName = c.ContactName
FROM Orders o
JOIN Customers c ON c.CustomerID = O.CustomerID
I suspect the problem is that you left out the FROM clause.
I left out INNER here, because this is implied.
I should add your original syntax is in alignment with ANSI standards,
whereas the syntax with FROM JOIN is proprietary to MS SQL Server and
Sybase (and possibly Informix). If you need portability, stick to the
original syntax. As long as you work with SQL Server only, do as you
please. Personally, I find the FROM/JOIN syntax very pleasant, as it
builds on the same paradigm as a regular SELECT statement. It is also
more effecient, if you need to update more than one column.
--
Erland Sommarskog, SQL Server MVP,
es****@sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp