我有以下 SQL:
UPDATE TableA
SET first_name = 'AAA',
last_name = 'BBB',
address1 = '123',
address2 = 'Fake St.,',
phone = '1234567',
id = '11223344'
如果每列不为空,我应该使用什么来更新每列?
我有以下 SQL:
UPDATE TableA
SET first_name = 'AAA',
last_name = 'BBB',
address1 = '123',
address2 = 'Fake St.,',
phone = '1234567',
id = '11223344'
如果每列不为空,我应该使用什么来更新每列?
update tableA
set first_name = case when first_name is null then null else 'aaa' end,
last_name = case when last_name is null then null else 'bbb' end,
...
只是另一种不那么冗长(且可读性较差)的方法:
UPDATE TableA
SET first_name = left( 'AAA' + first_name, 3 ) ,
last_name = left( 'BBB' + last_name, 3 ) ,
address1 = left( '123' + address1, 3 ) ,
address2 = left( 'Fake St.,' + address2, len( 'Fake St.,' ) ) ,
...
对于此示例,请使用 ISNULL 函数:
UPDATE TableA
SET first_name = ISNULL(first_name, NULL, 'AAA'),
last_name = ISNULL(last_name, NULL, 'BBB'),
(...)
GO