2017-06-16 41 views
0

每個月我們都會收到來自其他公司的文件,我們需要將其調整到我們的數據庫(在SQL Server中)。爲此我們運行幾個需要很長時間的更新。許多更新 - >一個更新?

有什麼辦法可以將所有這些更新「轉換」爲單個語句,因此它只能在表中運行一次? (這是一張非常大的桌子!)。

我們使用這樣的事情現在:

update Table1 set column01 = 0 where column01 = ' ' or column01 = ''; 
update Table1 set column02 = 0 where column02 = ' ' or column02 = ''; 
update Table1 set column03 = 0 where column03 = ' ' or column03 = ''; 
update Table1 set column04 = 0 where column04 = ' ' or column04 = ''; 
update Table1 set column05 = 0 where column05 = ' ' or column05 = ''; 
update Table1 set column06 = 0 where column06 = ' ' or column06 = ''; 
update Table1 set column07 = 0 where column07 = ' ' or column07 = ''; 
update Table1 set column08 = 0 where column08 = ' ' or column08 = ''; 
update Table1 set column09 = 0 where column09 = ' ' or column09 = ''; 
update Table1 set column10 = 0 where column10 = ' ' or column10 = ''; 
update Table1 set column11 = 0 where column11 = ' ' or column11 = ''; 
update Table1 set column12 = 0 where column12 = ' ' or column12 = ''; 
update Table1 set column13 = 0 where column13 = ' ' or column13 = ''; 
update Table1 set column14 = 0 where column14 = ' ' or column14 = ''; 
update Table1 set column15 = 0 where column15 = ' ' or column15 = ''; 
update Table1 set column16 = 0 where column16 = ' ' or column16 = ''; 
update Table1 set column17 = 0 where column17 = ' ' or column17 = ''; 
update Table1 set column18 = 0 where column18 = ' ' or column18 = ''; 
update Table1 set column19 = 0 where column19 = ' ' or column19 = ''; 

回答

0

您可以使用CASE ... WHEN這樣

update Table1 set 
    column01 = CASE when column01 = ' ' or column01 = '' then 0 ELSE column01 END, 
    column02 = CASE when column02 = ' ' or column02 = '' then 0 ELSE column02 END, 
...... 
    column19 = CASE when column19 = ' ' or column19 = '' then 0 ELSE column19 END 
+0

感謝TRIV,它的工作! – Miguel