SQL update from one Table to another based on a ID match

后端 未结 22 1344
太阳男子
太阳男子 2020-11-21 22:49

I have a database with account numbers and card numbers. I match these to a file to update any card numbers to the account number, so

22条回答
  •  眼角桃花
    2020-11-21 23:18

    For SQL Server 2008 + Using MERGE rather than the proprietary UPDATE ... FROM syntax has some appeal.

    As well as being standard SQL and thus more portable it also will raise an error in the event of there being multiple joined rows on the source side (and thus multiple possible different values to use in the update) rather than having the final result be undeterministic.

    MERGE INTO Sales_Import
       USING RetrieveAccountNumber
          ON Sales_Import.LeadID = RetrieveAccountNumber.LeadID
    WHEN MATCHED THEN
       UPDATE 
          SET AccountNumber = RetrieveAccountNumber.AccountNumber;
    

    Unfortunately the choice of which to use may not come down purely to preferred style however. The implementation of MERGE in SQL Server has been afflicted with various bugs. Aaron Bertrand has compiled a list of the reported ones here.

提交回复
热议问题