Oracle中的多个更新声明

时间:2016-03-05 13:18:11

标签: sql oracle

我们可以在Update

中执行多个SQL Server语句吗?

。我试过但发生了SQL Server错误。

错误

  

ORA-00907:缺少右括号

示例

UPDATE FACULTY SET Salary=(
12000 WHERE Faculty_ID=0163,
8,500 WHERE Faculty_ID=0164,
14,500 WHERE Faculty_ID=0165,
10,000 WHERE Faculty_ID=0166
);

2 个答案:

答案 0 :(得分:1)

使用CASE声明

UPDATE FACULTY
SET    Salary = CASE
                  WHEN Faculty_ID = '0163' THEN 12000
                  WHEN Faculty_ID = '0164' THEN 8500
                  WHEN Faculty_ID = '0165' THEN 14500
                  WHEN Faculty_ID = '0166' THEN 10000
                END
WHERE  Faculty_ID IN ( '0163', '0164', '0165', '0166' ) 

考虑到您不想更新其他Faculty_ID's

答案 1 :(得分:0)

我建议只写四个更新语句:

UPDATE FACULTY SET Salary = 12000 WHERE Faculty_ID = 0163;
UPDATE FACULTY SET Salary = 8500 WHERE Faculty_ID = 0164;
UPDATE FACULTY SET Salary = 14500 WHERE Faculty_ID = 0165;
UPDATE FACULTY SET Salary = 10000 WHERE Faculty_ID = 0166;

尽管您可以在一个语句中执行此操作,但逻辑更复杂,容易出错并且难以维护。使用Faculty_ID上的索引,四个查询的开销应该非常小。

注意数字和字符串。 SQL不会将逗号识别为数字中的分隔符,因此请勿使用它。此外,id中的前导零对于数字是不必要的。如果Faculty_ID是字符串,则使用单引号。