Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.databases.ms-sqlserver    |    Notorious Rube Goldberg contraption    |    19,505 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 19,349 of 19,505    |
|    Anton Shepelev to All    |
|    Re: UPDATE FROM    |
|    20 Jul 21 17:12:43    |
      From: antonius@freeshell.de              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: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca