Multiple SQL Update Statements in single query

后端 未结 6 1425
遥遥无期
遥遥无期 2020-12-17 16:23

I am in a situation where I am having to update about 12,000 items in my DB. Each row needs to mirror an excel file that I made previously. I have made the file that creates

6条回答
  •  粉色の甜心
    2020-12-17 16:42

    Take a look at MERGE e.g. something like:

    MERGE INTO [STORESQL].[dbo].[RPT_ITM_D] 
       USING (
              VALUES ('1.29', '0000000000001'),
                     ('1.39', '0000000000002')
             ) AS source (F1301, F01)
       ON F01 = source.F01
    WHEN MATCHED THEN
       UPDATE
          SET F1301 = source.F1301;
    

    ...but using a table value constructor in this way would not scale to 12,000 rows! So look to first copying the data from Excel to a table on the server, then use the table as the source e.g.

    MERGE INTO [STORESQL].[dbo].[RPT_ITM_D] 
       USING [STORESQL].[dbo].MyStagingTable AS source
          ON F01 = source.F01
    WHEN MATCHED THEN
       UPDATE
          SET F1301 = source.F1301;
    

提交回复
热议问题