使用过程创建包

时间:2013-08-10 22:51:16

标签: sql oracle

我正在尝试创建一个过程,其中输入是来自表Employee的lname,输出是sal和表信息中的平均值。两个输出都是数字,平均值是十进制数。这是我到目前为止所做的。

CREATE OR REPLACE PACKAGE getSalAvgPack IS PROCEDURE getSalAvg
(name IN Employee.lname%TYPE,
 pSal OUT NUMBER,
 pAvg OUT NUMBER);
END;
.
/

包裹编译得很好。

CREATE OR REPLACE PACKAGE BODY getSalAvgPack IS PROCEDURE getSalAvg
(name IN Employee.lname%TYPE,
 pSal OUT NUMBER,
 pAvg OUT NUMBER)
IS
BEGIN
  SELECT Information.sal, Information.average
  INTO pSal, pAvg
  FROM Information
  WHERE Information.eid=Employee.eid AND name=lname;
END;
END;
.
/

当我尝试编译包体时,我得到了这些错误

PL/SQL: SQL Statement ignored (for line 7, column 2)
PL/SQL: ORA-00904: "LNAME": invalid identifier

2 个答案:

答案 0 :(得分:1)

您需要加入Employee表:

  SELECT Information.sal, Information.average
  INTO pSal, pAvg
  FROM Information join
       Employee
       on Information.eid=Employee.eid
  WHERE name=lname;

答案 1 :(得分:1)

您似乎错过了加入

SELECT Information.sal, Information.average
  INTO pSal, pAvg
  FROM Information
       JOIN Employee ON (Information.eid = Employee.eid)
 WHERE name = Employee.lname;

作为一般原则,我强烈建议您为参数使用命名约定,以区别于表中的列。如果name是您要加入的任一表格中的列,则您的谓词会将Employee.lname与该列进行比较,而不是使用您的过程中的name参数。一个常见的约定是使用p_为所有参数添加前缀,以便您的查询变为

SELECT Information.sal, Information.average
  INTO p_Sal, p_Avg
  FROM Information
       JOIN Employee ON (Information.eid = Employee.eid)
 WHERE p_name = Employee.lname;