如何在包装体内解决这个问题?

时间:2018-10-27 12:06:03

标签: oracle plsql

如何在包装体内解决此问题?

函数get_amount应该返回acc_id的acc_amount

函数get_date应该返回acc_id为acc_id

如何使用功能

代码中有错误的地方

CREATE OR REPLACE PACKAGE BODY account_api AS
    PROCEDURE add_new_account
        ( p_acc_id     accounts.acc_id%type
        , p_acc_name   accounts.acc_name%type
        , p_acc_amount accounts.acc_amount%type
        , p_acc_date   accounts.acc_date%type)
    IS
    BEGIN
        INSERT INTO account (acc_id, acc_name, acc_amount, acc_date)
        VALUES (acc_seq.nextval, p_acc_id, p_acc_name, p_acc_amount, p_acc_date);
    COMMIT;
    EXCEPTION
            WHEN OTHERS THEN
            ROLLBACK;
            RAISE;
    END;
    PROCEDURE upd_account
        (
          p_acc_id     accounts.acc_id%type
        , p_acc_name   accounts.acc_name%type
        , p_acc_amount accounts.acc_amount%type
        , p_acc_date   accounts.acc_date%type
    )
    IS
    BEGIN
        UPDATE accounts
            SET acc_naziv  = p_acc_naziv
              , acc_amount = p_acc_amount
              , acc_date   = p_acc_date
        WHERE acc_id = p_acc_id;
    COMMIT;
    END;
    PROCEDURE del_accounts
        (p_acc_id     accounts.acc_id%type)
    DELETE FROM accounts WHERE acc_id = p_acc_id;
    COMMIT;
    EXCEPTION
      WHEN OTHERS THEN
      ROLLBACK;
      RAISE;
    END;
    FUNCTION get_amount
        (p_acc_id    accounts.acc_id%type)
    return Number is res number;
    begin 
        select acc_amount into res 
        from  account where acc_id =p_acc_id; 
        return res;
    end;
    FUNCTION get_date
        (p_acc_id    accounts.acc_id%type)
    RETURN date IS res1 date;
    BEGIN
        SELECT acc_date INTO res1
        FROM accounts WHERE acc_id = p_acc_id;
        RETURN res1;
        end;
   end account_api;
     /

1 个答案:

答案 0 :(得分:1)

  • 首先,您需要确定表名称accountsaccount。我假设将accounts用于已编辑的代码块,它将 出现在下面。

  • 对于PROCEDURE add_new_account,值的参数数量
    list大于列名列表的参数数量。

  • 对于PROCEDURE upd_account,没有定义为 p_acc_naziv在UPDATE语句中声明。
  • 缺少PROCEDURE del_accounts IS BEGIN部分

package header之前应该有一个package body,如下所示:

CREATE OR REPLACE PACKAGE account_api AS
    PROCEDURE add_new_account
        ( p_acc_id     accounts.acc_id%type
        , p_acc_name   accounts.acc_name%type
        , p_acc_amount accounts.acc_amount%type
        , p_acc_date   accounts.acc_date%type);

    PROCEDURE upd_account
        (
          p_acc_id     accounts.acc_id%type
        , p_acc_name   accounts.acc_name%type
        , p_acc_amount accounts.acc_amount%type
        , p_acc_date   accounts.acc_date%type
    );
    PROCEDURE del_accounts
        (p_acc_id     accounts.acc_id%type);

    FUNCTION get_amount
        (p_acc_id    accounts.acc_id%type)
    return Number;
    FUNCTION get_date
        (p_acc_id    accounts.acc_id%type)
    RETURN date;
end account_api;

package body应该为:

CREATE OR REPLACE PACKAGE BODY account_api AS
    PROCEDURE add_new_account
        ( p_acc_id     accounts.acc_id%type
        , p_acc_name   accounts.acc_name%type
        , p_acc_amount accounts.acc_amount%type
        , p_acc_date   accounts.acc_date%type)
    IS
    BEGIN
        INSERT INTO accounts(acc_id, acc_name, acc_amount, acc_date)
        VALUES (-- acc_seq.nextval, --> need to be removed
                p_acc_id, p_acc_name, p_acc_amount, p_acc_date);
    COMMIT;
    EXCEPTION
            WHEN OTHERS THEN
            ROLLBACK;
            RAISE;
    END;
    PROCEDURE upd_account
        (
          p_acc_id     accounts.acc_id%type
        , p_acc_name   accounts.acc_name%type
        , p_acc_amount accounts.acc_amount%type
        , p_acc_date   accounts.acc_date%type
    )
    IS
    BEGIN
        UPDATE accounts
            SET /*acc_naziv  = p_acc_naziv  -->  there's no identifier defined as p_acc_naziv
                                            -->> suppose you'd use "acc_name = p_acc_name" here.  
              , */acc_amount = p_acc_amount
              , acc_date   = p_acc_date
        WHERE acc_id = p_acc_id;
    COMMIT;
    END;
    PROCEDURE del_accounts
        (p_acc_id     accounts.acc_id%type)
    IS     -->  missing
    BEGIN  -->  missing
    DELETE FROM accounts WHERE acc_id = p_acc_id;
    COMMIT;
    EXCEPTION
      WHEN OTHERS THEN
      ROLLBACK;
      RAISE;
    END;
    FUNCTION get_amount
        (p_acc_id    accounts.acc_id%type)
    return Number is res number;
    begin
        select acc_amount into res
        from  accounts where acc_id =p_acc_id;
        return res;
    end;
    FUNCTION get_date
        (p_acc_id    accounts.acc_id%type)
    RETURN date IS res1 date;
    BEGIN
        SELECT acc_date INTO res1
        FROM accounts WHERE acc_id = p_acc_id;
        RETURN res1;
        end;
   end account_api;