我有更新表格的存储过程:
UPDATE myTbl
SET [first_name] = @first_name,
[second_name] = @second_name,
[faculty] = @faculty,
[age] = @age
WHERE [id] = @id
当我调用存储过程并传递所有更新的参数时。现在我想更新表,所以如果我没有输入要更新,更新的所有值,只有我介绍的那些,以及未引入的值保持不变。
答案 0 :(得分:5)
UPDATE myTbl
SET [first_name] = IsNull(@first_name, first_name),
[second_name] = IsNull(@second_name, second_name),
[faculty] = IsNull(@faculty, faculty),
[age] = IsNull(@age, age),
WHERE [id] = @id
答案 1 :(得分:3)
UPDATE myTbl
SET
[first_name] = ISNULL(@first_name, [first_name])
, [second_name] = ISNULL(@second_name, [second_name])
, [faculty] = ISNULL(@faculty, [faculty])
, [age] = ISNULL(@age, [age])
WHERE [id] = @id
答案 2 :(得分:0)
在运行更新之前,您可以运行SELECT语句从表中提取所有值。
此外,您验证传递给存储过程的所有值,如果它们为null,则使用更新语句中的选定值。