14

我有以下 SQL:

UPDATE TableA
SET first_name = 'AAA',
    last_name = 'BBB',
    address1 = '123',
    address2 = 'Fake St.,',
    phone = '1234567',
    id = '11223344'

如果每列不为空,我应该使用什么来更新每列?

4

3 回答 3

33
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,
...
于 2012-08-27T09:13:04.493 回答
1

只是另一种不那么冗长(且可读性较差)的方法:

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.,' ) )  ,
    ...
于 2012-08-27T09:28:07.663 回答
0

对于此示例,请使用 ISNULL 函数:

UPDATE TableA
SET first_name = ISNULL(first_name, NULL, 'AAA'),
    last_name = ISNULL(last_name, NULL, 'BBB'),
    (...)
GO
于 2021-12-09T22:07:24.633 回答