单表的批量SQL更新

mor*_*rck 0 sql sql-server

我在表格中进行35050行的批量更新时遇到问题,我需要更新前面没有零的所有条形码.我有的SQL SELECT语句是这样的.它可以正常工作,但我的更新声明没有.

SELECT * 
FROM [Hep_ManEquipInterface].[dbo].[LabelUnitBarcodes] h WITH (NOLOCK) 
WHERE LEFT(h.barcode, 1) != '0'
ORDER BY [created_at] desc
Run Code Online (Sandbox Code Playgroud)

哪个给我这张桌子.

barcode         weight  created_at                location  SupplierType
409101685473    2285    2015-02-02 12:23:03.830   372   
409101275155    2285    2015-02-02 12:22:51.143   372   
409101685466    2285    2015-02-02 12:22:34.983   372   
409101275148    2285    2015-02-02 12:22:25.280   372   
Run Code Online (Sandbox Code Playgroud)

结果应该是条形码前面为零:

barcode         weight  created_at                location  SupplierType
0409101685473   2285    2015-02-02 12:23:03.830   372   
0409101275155   2285    2015-02-02 12:22:51.143   372   
0409101685466   2285    2015-02-02 12:22:34.983   372   
0409101275148   2285    2015-02-02 12:22:25.280   372   
Run Code Online (Sandbox Code Playgroud)

我当然试过但是我一直在收到错误然后使用这个声明:

UPDATE [Hep_ManEquipInterface].[dbo].[LabelUnitBarcodes]
SET [barcode] = '0'+ (SELECT * h.[barcode] FROM [Hep_ManEquipInterface].[dbo].[LabelUnitBarcodes] h WITH (NOLOCK) WHERE LEFT(h.barcode, 1) != '0')
Run Code Online (Sandbox Code Playgroud)

jar*_*rlh 5

不要让事情复杂化,这只是一个简单的更新.

UPDATE [Hep_ManEquipInterface].[dbo].[LabelUnitBarcodes]
SET [barcode] = '0'+ [barcode]
WHERE LEFT(barcode, 1) != '0'
Run Code Online (Sandbox Code Playgroud)