0

我有一个案例陈述:

UPDATE
   Answer
SET
   AnswerID = @AnswerID,
   AnsweredBy = CASE WHEN LEN(@AnsweredBy) > 0 THEN @AnsweredBy END

即使什么时候@AnsweredByNULL仍然将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 列,但只更新answeredbyif 中是否有值@AnsweredBy

4

2 回答 2

6
SET AnsweredBy = ISNULL(@AnsweredBy, AnsweredBy)
于 2012-06-26T14:49:32.370 回答
2
UPDATE 
    Answer 
SET 
    AnswerID = @AnswerID, 
    AnsweredBy = CASE WHEN LEN(@AnsweredBy) > 0 THEN @AnsweredBy ELSE AnsweredBy END 
于 2012-06-26T14:50:24.767 回答