Erland Sommarskog to Anton Shepelev:
The simplest UPDATE from another table does *not* requre
that it the table begin updated be mentioned in the FROM
clause:
UPDATE upd_tab
SET col = data_tab.col
FROM data_tab
WHERE data_tab.code = upd_tab.code
Whereas this is legal and produces something, I definitely
recommend against it. I will have to admit that I don't
understand what this is doing - and I certainly play an
SQL expert on TV.
I had been of simlar opinion until I tested that code. Then
I pondered it some more and concluded that it is clear,
logical, and correct. See for yourself:
CREATE TABLE #upd_tab (code INT, col INT)
CREATE TABLE #data_tab(code INT, col INT)
INSERT INTO #upd_tab VALUES
(8, 0),(1, 0),(7, 0),(2, 0),
(6, 0),(3, 0),(5, 0),(4, 0)
INSERT INTO #data_tab VALUES
(1, 1),(2, 2),(3, 3),(4, 4),
(5, 5),(6, 6),(7, 7),(8, 8)
SELECT * FROM #upd_tab
UPDATE #upd_tab
SET col = #data_tab.col
FROM #data_tab
WHERE #data_tab.code = #upd_tab.code
SELECT * FROM #upd_tab
DROP TABLE #upd_tab
DROP TABLE #data_tab
--
() ascii ribbon campaign - against html e-mail
/\
http://preview.tinyurl.com/qcy6mjc [archived]
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)