PL / SQL中带有标识符的错误

时间:2013-01-14 08:59:54

标签: oracle plsql

我正在尝试运行一些PL / SQL代码,但它包含一些关于标识符的错误,请帮助我。代码未运行

DECLARE
   a := customer.purchase%TYPE;
   id := &employee.empno;
BEGIN
   UPDATE employee SET salary = salary + 5000;
   UPDATE employee SET bonus = bonus + 1000 WHERE empno = &id;
   SAVEPOINT sumeet;
   UPDATE customer SET purchase = purchase + 5000 WHERE custid = a;

   SELECT SUM(purchase) INTO a;

   IF (a < 11000) THEN
      ROLLBACK sumeet;
   END IF;
   COMMIT;

END;
/

4 个答案:

答案 0 :(得分:1)

除了Alen的修复,而不是ROLLBACK为什么不这样做:

   UPDATE customer SET purchase = purchase + 5000 
    WHERE custid = a
      AND (select sum(purchase) from customer) + 5000 < 11000;

  COMMIT;

答案 1 :(得分:1)

1:这是错误的:

DECLARE
  a := customer.purchase%TYPE;
  id := &employee.empno;

您没有在变量名称后添加:=&无效,employee.empno不是有效的数据类型。即:

DECLARE
  a customer.purchase%TYPE;
  id employee.empno%TYPE;

2:您不需要&来引用id变量:

UPDATE employee SET bonus = bonus + 1000 WHERE empno = &id;

即:

UPDATE employee SET bonus = bonus + 1000 WHERE empno = id;

答案 2 :(得分:0)

尝试使用此块,但首先更改声明块中的值(1,2)。

DECLARE
   a customer.purchase%TYPE := 1;
   id employee.empno%TYPE := 2;
BEGIN
   UPDATE employee SET salary = salary + 5000;
   UPDATE employee SET bonus = bonus + 1000 WHERE empno = id;
   SAVEPOINT sumeet;
   UPDATE customer SET purchase = purchase + 5000 WHERE custid = a;

   SELECT SUM(purchase) INTO a FROM customer;

   IF (a < 11000) THEN
      ROLLBACK sumeet;
   END IF;
   COMMIT;

END;
/

答案 3 :(得分:0)

这里有一个提示:将员工的两个更新合并为一个。

 UPDATE employee
 SET    salary = salary + 5000,
        bonus  = bonus  + case when empno = &id then 1000 else 0 end;

此外,开始使用有意义的变量名称。