有我更新表的存储过程:
UPDATE myTbl
SET [first_name] = @first_name,
[second_name] = @second_name,
[faculty] = @faculty,
[age] = @age
WHERE [id] = @id
当我调用存储过程并传递更新工作的所有参数时。现在我想更新表格,所以如果我不输入所有要更新的值,更新后,只有我引入的和没有引入的保持不变。
有我更新表的存储过程:
UPDATE myTbl
SET [first_name] = @first_name,
[second_name] = @second_name,
[faculty] = @faculty,
[age] = @age
WHERE [id] = @id
当我调用存储过程并传递更新工作的所有参数时。现在我想更新表格,所以如果我不输入所有要更新的值,更新后,只有我引入的和没有引入的保持不变。
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
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
在运行更新之前,您可以运行 SELECT 语句以从表中提取所有值。
此外,您验证传递给存储过程的所有值,如果它们为空,则在更新语句中使用选定的值。