算术时忽略SQL语句

时间:2014-04-17 11:07:40

标签: sql oracle

我一直在PL / SQL中尝试一些算法。但是,在添加,乘以等数组的两个元素时,我在算术中遇到了问题。

我的阵列是效率,薪水,现金。所有都是四个元素。我有以下问题。每当我按以下方式划分这两个元素时,

EFFICIENCY(2):=SALARIES(2)/CASH(2);    

我得到SQL statement ignored error。为什么会这样?

P.S:我在循环中划分元素,即EFFICIENCY(I):=SALARIES(I)/CASH(I);

我发布了整个源代码:

CREATE OR REPLACE PROCEDURE ANALYSIS
IS
CURSOR EMPLOYEES IS
 SELECT * FROM EMPLOYEE;
CURSOR BILLS IS
 SELECT * FROM BILL;
WORKER EMPLOYEE%ROWTYPE;
CASH BILL%ROWTYPE;
TYPE SALARY IS VARRAY(4) OF NUMBER(6,2);
SALARIES SALARY:=SALARY(0,0,0,0);
AMOUNTS SALARY:=SALARY(0,0,0,0);
EFFICIENCY SALARY:=SALARY(0,0,0,0);

BEGIN 

 OPEN EMPLOYEES;
 OPEN BILLS;
 LOOP
  FETCH EMPLOYEES INTO WORKER;
  EXIT WHEN EMPLOYEES%NOTFOUND;
  IF WORKER.BRANCHID=1 THEN 
   SALARIES(1):=SALARIES(1)+WORKER.SALARY;
  ELSIF WORKER.BRANCHID=2 THEN 
   SALARIES(2):=SALARIES(2)+WORKER.SALARY;
  ELSIF WORKER.BRANCHID=3 THEN 
   SALARIES(3):=SALARIES(3)+WORKER.SALARY;
  ELSIF WORKER.BRANCHID=4 THEN 
   SALARIES(4):=SALARIES(4)+WORKER.SALARY;
  END IF;
 END LOOP;
 LOOP
  FETCH BILLS INTO CASH;
  EXIT WHEN BILLS%NOTFOUND;
  IF CASH.BRANCHID=1 THEN
   AMOUNTS(1):=AMOUNTS(1)+CASH.AMOUNT;
  ELSIF CASH.BRANCHID=2 THEN
   AMOUNTS(2):=AMOUNTS(2)+CASH.AMOUNT;
  ELSIF CASH.BRANCHID=3 THEN
   AMOUNTS(3):=AMOUNTS(3)+CASH.AMOUNT;
  ELSIF CASH.BRANCHID=4 THEN
   AMOUNTS(4):=AMOUNTS(4)+CASH.AMOUNT;
  END IF;
 END LOOP;

 FOR I IN 1..4 LOOP
  EFFICIENCY(I):=SALARIES(I)/CASH(I);
  DBMS_OUTPUT.PUT_LINE(I);
 END LOOP;
END;

2 个答案:

答案 0 :(得分:2)

您正在使用CASH,就好像它是一个数组:

EFFICIENCY(I):=SALARIES(I)/CASH(I);

但它不是一个数组:

CASH BILL%ROWTYPE;

所以你得到:

LINE/COL ERROR
-------- -----------------------------------------------------------------
43/3     PL/SQL: Statement ignored
43/30    PLS-00222: no function with name 'CASH' exists in this scope

我不知道您为什么只看到第一条错误消息?

答案 1 :(得分:1)

您应该看到PLS-00222: no function with name 'CASH' exists in this scope错误。

CASH定义为BILL%ROWTYPE,而不是数组,因此您无法在算术中引用CASH(I)。我不确定你的意图是什么,所以我们不清楚它应该如何修改,但是我认为你应该在那里引用AMOUNTS

EFFICIENCY(2):=SALARIES(2)/AMOUNTS(2);