Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Need some SQL syntax help :-)

Both databases are on the same server

db1 = DHE db2 = DHE_Import  UPDATE DHE.dbo.tblAccounts  INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink      ON DHE.dbo.tblAccounts.AccountCode = DHE_Import.tblSalesRepsAccountsLink.AccountCode  SET DHE.dbo.tblAccounts.ControllingSalesRep = DHE_Import.dbo.tblSalesRepsAccountsLink.SalesRepCode 

I can do a query in Access with linked tables with similar syntax - BUT SQL doesn't like it.

I'm sure it's a simple issue :-D

Thanks!

like image 221
Charlez Avatar asked Mar 27 '12 17:03

Charlez


People also ask

Can we use inner join in update query?

SQL Server UPDATE JOIN syntax To query data from related tables, you often use the join clauses, either inner join or left join. In SQL Server, you can use these join clauses in the UPDATE statement to perform a cross-table update.

Can we join two tables from different databases in SQL Server?

SQL Server allows you to join tables from different databases as long as those databases are on the same server. The join syntax is the same; the only difference is that you must fully qualify table names.


1 Answers

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 
like image 119
jerry Avatar answered Oct 07 '22 18:10

jerry