0

有我更新表的存储过程:

UPDATE myTbl
SET [first_name] = @first_name,  
    [second_name] = @second_name,  
    [faculty] = @faculty,
    [age] = @age
    WHERE [id] = @id 

当我调用存储过程并传递更新工作的所有参数时。现在我想更新表格,所以如果我不输入所有要更新的值,更新后,只有我引入的和没有引入的保持不变。

4

3 回答 3

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 
于 2013-06-17T12:50:43.990 回答
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 
于 2013-06-17T12:50:53.070 回答
0

在运行更新之前,您可以运行 SELECT 语句以从表中提取所有值。

此外,您验证传递给存储过程的所有值,如果它们为空,则在更新语句中使用选定的值。

于 2013-06-17T12:51:07.823 回答