PL / SQL查询错误

时间:2016-04-08 19:45:40

标签: sql plsql

尝试修复我的PL / SQL查询,并在出现问题时需要一些帮助。我的代码如下:

DECLARE
   TYPE lv_student_ID IS TABLE OF NUMBER;

BEGIN
   -- assuming you are trying to find all students who have not yet paid, you can use a nested table
   SELECT Lease.StudentIDNumber INTO lv_student_ID
     FROM Lease 
     INNER JOIN Invoice 
     ON Lease.LeaseID = Invoice.LeaseID
    WHERE IsPaid = 'N';

END;

当我运行时,我得到的错误如下:

Error report -
ORA-06550: line 6, column 38:
PLS-00321: expression 'LV_STUDENT_ID' is inappropriate as the left hand side of an assignment statement
ORA-06550: line 7, column 6:
PL/SQL: ORA-00904: : invalid identifier
ORA-06550: line 6, column 4:
PL/SQL: SQL Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

要纠正您的紧急问题,您必须认识到您正在尝试选择标量值(number)到集合类型table of number ),这不起作用。要使其正常工作,首先需要声明该类型的变量。然后,您可以向查询添加a bulk collect clause,这会将select的语义更改为选择集合。

DECLARE
   TYPE lv_student_ID IS TABLE OF NUMBER;
   student_ids lv_student_id;
BEGIN
    SELECT Lease.StudentIDNumber
    BULK COLLECT INTO student_ids
    FROM Lease 
    INNER JOIN Invoice 
    ON Lease.LeaseID = Invoice.LeaseID
    WHERE IsPaid = 'N';
END;

如果您只想要那些没有租金的学生,那么您可以使用该集合的.count属性:student_ids.COUNT。但更简单的解决方案就是使用简单的count(*)聚合查询。

DECLARE
   num_students NUMBER;
BEGIN
   SELECT count(*) INTO num_students
   FROM Lease 
   INNER JOIN Invoice 
   ON Lease.LeaseID = Invoice.LeaseID
   WHERE IsPaid = 'N';
END;

答案 1 :(得分:0)

鉴于您正在尝试计算尚未支付租金的学生人数,我希望您使用COUNT功能。将所有这些变量加载到TABLE类型变量中(正如Gordon所说,你仍然需要首先声明该类型的变量)会给你一个学生ID列表,然后你必须得到他们的计数。< / p>

这是相当基本的东西。尝试在Oracle文档中搜索COUNT函数,这将为您提供一个很好的示例。