Update Query with INNER JOIN between tables in 2 different databases on 1 server Update Query with INNER JOIN between tables in 2 different databases on 1 server sql-server sql-server

Update Query with INNER JOIN between tables in 2 different databases on 1 server


You could call it just style, but I prefer aliasing to improve readability.

UPDATE A      SET ControllingSalesRep = RA.SalesRepCode   from DHE.dbo.tblAccounts A  INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink RA    ON A.AccountCode = RA.AccountCode

For MySQL

UPDATE DHE.dbo.tblAccounts A   INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink RA       ON A.AccountCode = RA.AccountCode SET A.ControllingSalesRep = RA.SalesRepCode


Following is the MySQL syntax:

UPDATE table1 INNER JOIN table2 ON table1.field1 = table2.field2SET table1.field3 = table2.field4 WHERE ...... ;

http://geekswithblogs.net/faizanahmad/archive/2009/01/05/join-in-sql-update--statement.aspx


Sorry its late, but I guess it would be of help to those who land here finding a solution to similar problem. The set clause should come right after the update clause. So rearranging your query with a bit change does the work.

UPDATE DHE.dbo.tblAccounts SET DHE.dbo.tblAccounts.ControllingSalesRep    = DHE_Import.dbo.tblSalesRepsAccountsLink.SalesRepCodefrom DHE.dbo.tblAccounts INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink     ON DHE.dbo.tblAccounts.AccountCode        = DHE_Import.tblSalesRepsAccountsLink.AccountCode