答案 0 :(得分:3)
UPDATE [dbo].[TempTable] SET [Level1] = CASE WHEN A = -1 THEN 0 ELSE A END ,[Level2] = CASE WHEN A = -1 THEN 0 ELSE A END;
在这种情况下,您需要使用CASE
。尝试上面的查询。
答案 1 :(得分:0)
如果您坚持使用 IF ,则可以尝试以下操作:
IF
IF A = -1 BEGIN UPDATE [dbo].[TempTable] SET [Level1] = 0 ,[Level2] = 0 END ELSE BEGIN UPDATE [dbo].[TempTable] SET [Level1] = A ,[Level2] = A END
IIF
UPDATE [dbo].[TempTable] SET [Level1] = IIF(A = -1, 0, A) ,[Level2] = IIF(A = -1, 0, A)
答案 2 :(得分:0)
您可以使用CASE语句,并且如果dbo.TempTable中的列之一为“ A”
DECLARE @Table TABLE(A INT,Level1 INT,Level2 INT)
INSERT @Table
SELECT -1, NULL, NULL UNION ALL
SELECT 1,NULL,NULL
UPDATE t SET
Level1 = CASE WHEN A = -1 THEN 0 ELSE A END,
Level2 = CASE WHEN A = -1 THEN 0 ELSE A END
FROM @Table t
SELECT * FROM @Table
-结果
A Level1 Level2
-1 0 0
1 1 1
答案 3 :(得分:0)
有大小写或IIF;)
UPDATE [dbo].[TempTable]
SET [Level1] = IIF(A = -1 , 0 , A), [Level2] = IIF(A = -1 , 0 , A)
答案 4 :(得分:0)
使用 IIF
进行检查:
UPDATE [dbo].[TempTable]
SET [Level1] = IIF(A = -1, 0, A)
,[Level2] = IIF(A = -1, 0, A);