Home > other >  I can't create and using a Package
I can't create and using a Package

Time:04-01

CREATE OR REPLACE PACKAGE Learning_package AS

    PROCEDURE add_person(name_person people.C_Name%type,
    mail_person people.C_mail%type,
    age_person  people.C_age%TYPE);
END Learning_package;

CREATE OR REPLACE PACKAGE BODY Learning_package AS

    PROCEDURE add_person(name_person  people.C_Name%type,
    mail_person  people.C_mail%type,
    age_person   people.C_age%TYPE)
    IS
    BEGIN
         INSERT INTO people (C_Name, C_mail, C_age)
          VALUES (name_person, mail_person, age_person);  
    END add_person;

END Learning_package;

/
BEGIN
    Learning_package.add_person('john', '[email protected]', 20);
END;

an error appears:

package body "Learning_package" has erros PL/SQL: could not find program unit being called "Learning_package" at line 3

Can someone help me?

CodePudding user response:

Could you also add a block terminator after the package creation and change the AS to IS (/).

CREATE OR REPLACE PACKAGE Learning_package IS

END Learning_package;
/

Same goes for the package body.

CodePudding user response:

Your code is fine except that you need to:

  • Make sure that you have a people table with the expected columns; and
  • Use a / statement terminator in a new line at the end of every PL/SQL statement.

Like this:

CREATE OR REPLACE PACKAGE Learning_package AS

    PROCEDURE add_person(name_person people.C_Name%type,
    mail_person people.C_mail%type,
    age_person  people.C_age%TYPE);
END Learning_package;
/

CREATE OR REPLACE PACKAGE BODY Learning_package AS

    PROCEDURE add_person(name_person  people.C_Name%type,
    mail_person  people.C_mail%type,
    age_person   people.C_age%TYPE)
    IS
    BEGIN
         INSERT INTO people (C_Name, C_mail, C_age)
          VALUES (name_person, mail_person, age_person);  
    END add_person;

END Learning_package;
/

BEGIN
    Learning_package.add_person('john', '[email protected]', 20);
END;
/

Note: Age is not a good thing to store as it quickly becomes out-of-date. Instead, you should store date-of-birth and when you need an age you can calculate it from the date-of-birth and today's date.

db<>fiddle here

  • Related