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

后端 未结 11 1261
梦如初夏
梦如初夏 2020-12-08 09:29

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         


        
相关标签:
11条回答
  • 2020-12-08 09:36

    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
    
    0 讨论(0)
  • 2020-12-08 09:36

    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.SalesRepCode
    from DHE.dbo.tblAccounts 
    INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink 
        ON DHE.dbo.tblAccounts.AccountCode
            = DHE_Import.tblSalesRepsAccountsLink.AccountCode 
    
    0 讨论(0)
  • 2020-12-08 09:38
    Update one table using Inner Join
    
      UPDATE Table1 SET name=ml.name
    FROM table1 t inner JOIN
    Table2 ml ON t.ID= ml.ID  
    
    0 讨论(0)
  • 2020-12-08 09:39
    //For Access Database:
    UPDATE ((tblEmployee
    LEFT JOIN tblCity ON (tblEmployee.CityCode = tblCity.CityCode))
    LEFT JOIN tblCountry ON (tblEmployee.CountryCode = tblCountryCode))
    SET tblEmployee.CityName = tblCity.CityName, 
    tblEmployee.CountryName = tblCountry.CountryName
    WHERE (tblEmployee.CityName = '' OR tblEmployee.CountryName = '')
    
    0 讨论(0)
  • 2020-12-08 09:40

    It is explained here http://erabhinavrana.blogspot.in/2014/01/how-to-execute-update-query-by-applying.html

    It also has other useful code snippets which are commonly used.

    update <dbname of 1st table>.<table name of 1st table> A INNER JOIN <dbname of 2nd table>.<table name of 2nd table> RA ON A.<field name of table 1>=RA.<field name of table 2> SET A.<field name of table 1 to be updated>=RA.<field name of table 2 to set value in table 1>
    

    Replace data in <> with your appropriate values.

    That's It. source:

    http://www.dynamic-coders.com/how-to-update-two-different-tables-in-different-databases-on-same-server

    0 讨论(0)
  • 2020-12-08 09:41

    Worked perfectly for me.

    UPDATE TABLE_A a INNER JOIN TABLE_B b ON a.col1 = b.col2 SET a.col_which_you_want_update = b.col_from_which_you_update;
    
    0 讨论(0)
提交回复
热议问题