我有一个案例陈述:
UPDATE
Answer
SET
AnswerID = @AnswerID,
AnsweredBy = CASE WHEN LEN(@AnsweredBy) > 0 THEN @AnsweredBy END
即使什么时候@AnsweredBy
它NULL
仍然将AnsweredBy
列设置为空。
我也尝试通过这样做来测试 null :
UPDATE
Answer
SET
AnswerID = @AnswerID,
AnsweredBy = CASE WHEN @AnsweredBy IS NOT NULL THEN @AnsweredBy END
意思是我不想更新answeredby
列,除非有值。
即使在我的 C# 代码中,我也不会将值传递给我的存储过程:
if (answeredBy.Length > 0)
cmdSelect.Parameters.Add("@AnsweredBy", SqlDbType.VarChar).Value = answeredBy;
除非有值。在我的存储过程中,我将该列变量默认为空:
@RunoffAnswerID bigint,
@AnswerID varchar(3)=NULL,
@AnsweredBy varchar(50)=NULL,
所以我的问题是如何执行其余的更新,因为还有大约 5-10 列,但只更新answeredby
if 中是否有值@AnsweredBy
?