如何在PL / SQL中调用打包函数?

时间:2013-12-21 20:54:29

标签: oracle plsql

我创建了一个包规范和正文:

CREATE OR REPLACE PACKAGE pack_debt AS
  overdue_charge CONSTANT number :=0.005;
  FUNCTION debt (person_nr IN number) RETURN number;
end pack_debt;

CREATE OR REPLACE PACKAGE BODY pack_debt AS
  FUNCTION debt (person_nr IN number)
    RETURN number IS
    overdue number;
  BEGIN
    select (date_of_return-hiredate)*(price*overdue_charge) into overdue 
      from books, rentals, users  
     where users.id = rentals.user_id 
       and books.id = rentals.book_id 
       and date_of_return - hiredate > 14 
       and users.id = person_nr;
  end debt;
end pack_debt;

我想看到过期的值,但是当我尝试调用该函数时,它不起作用。应该怎么调用?

如果我只是创建函数:

create or replace function dept (person_nr IN number)
  RETURN number IS
  overdue number;
begin
  select (date_of_return - hiredate) * (price * 0.005) into overdue 
    from books, rentals, users 
   where users.id = rentals.user_id 
     and books.id = rentals.book_id 
     and date_of_return - hiredate > 14 
     and users.id = person_nr;
  RETURN (overdue);
end dept;

并且做.. ..

select dept('1') from dual;

我得到了逾期的价值。如何在使用包时获得相同的东西?

2 个答案:

答案 0 :(得分:0)

当pp单独编写或封装在Package中时,

FUNCTION块语法非常相似。 正如@Ben所说,RETURN语句是强制性的。

您声明FUNCTION,就像RETURN NUMBER一样,但没有指定函数返回的变量或常量,所以它不完整!您的Package Body保留INVALID

最后称它非常简单,

SELECT pack_debt.debt('1') FROM DUAL;

答案 1 :(得分:0)

请检查代码。我认为它应该有用。

CREATE OR REPLACE PACKAGE pack_debt AS
      overdue_charge CONSTANT number :=0.005;
      FUNCTION debt (person_nr IN number) RETURN number;
    end pack_debt;

    CREATE OR REPLACE PACKAGE BODY pack_debt AS
      FUNCTION debt (person_nr IN number)
        RETURN number AS
        overdue number;
      BEGIN
        select SALARY into overdue from avrajit
    where SR_NO = person_nr;
    RETURN overdue;
      end debt;
    end pack_debt;
    ------------------------------------
    DECLARE
    AV_NO NUMBER;
    BEGIN
    AV_NO:= pack_debt.debt(2);
    DBMS_OUTPUT.PUT_LINE(AV_NO);
    END;