Update e Inner Join in SQL

Si… lo so… ormai il mondo dello sviluppo si muove su altre strade (ORM, CQRS, DB documentali), ma a volte abbiamo a che fare con il buon vecchio SQL.

Ogni volta che aggiungo una anagrafica dove prima c’era il “codice secco” non mi ricordo come aggironare in una sola volta tutti gli id della tabella…

…beh ecco un memo se siete come me Winking smile

UPDATE b 
SET    b.article_id = a.id 
FROM   billofmaterials b 
       INNER JOIN articles a 
               ON b.articlecode = a.code 
WHERE  b.article_id IS NULL 
Technorati Tags: ,,

posted @ mercoledì 23 ottobre 2013 19:14

Print

Comments on this entry:

# re: Update e Inner Join in SQL

Left by Roberto Sarati at 24/10/2013 00:52
Gravatar
Ciao,
non so la versione di SQL Server che stai usando, ma dal 2008 è possibile usare il comando MERGE che è ANSI standard e, imho, più potente, vesatile e "leggibile".

La tua query si trasformerebbe in:

MERGE INTO billofmaterials AS [target]
USING articles AS [source]
ON([target].article_code = [source].code)
WHEN MATCHED AND [target].article_id IS NULL THEN
update set [target].article_id = [source].id;

Ciao.

# re: Update e Inner Join in SQL

Left by Nazareno at 24/10/2013 18:37
Gravatar
Grazie! Sul leggibile direi che siamo pari ;)
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011