Call function in a Package : Package Body « Function Procedure Packages « Oracle PL/SQL Tutorial






SQL>
SQL>
SQL>
SQL> create table company(
  2     product_id        number(4)    not null,
  3     company_id          NUMBER(8)    not null,
  4     company_short_name  varchar2(30) not null,
  5     company_long_name   varchar2(60)
  6  );

Table created.

SQL> insert into company values(1,1001,'A Inc.','Long Name A Inc.');

1 row created.

SQL> insert into company values(1,1002,'B Inc.','Long Name B Inc.');

1 row created.

SQL> insert into company values(1,1003,'C Inc.','Long Name C Inc.');

1 row created.

SQL> insert into company values(2,1004,'D Inc.','Long Name D Inc.');

1 row created.

SQL> insert into company values(2,1005,'E Inc.','Long Name E Inc.');

1 row created.

SQL> insert into company values(2,1006,'F Inc.','Long Name F Inc.');

1 row created.

SQL>
SQL>
SQL>
SQL>
SQL> CREATE OR REPLACE PACKAGE myPackage
  2  IS
  3    FUNCTION getName(ip_product_id NUMBER,ip_company_id NUMBER)
  4    RETURN VARCHAR2;
  5    PRAGMA RESTRICT_REFERENCES(getName,WNDS,WNPS);
  6  END myPackage;
  7  /

Package created.

SQL> CREATE OR REPLACE PACKAGE BODY myPackage
  2  IS
  3    FUNCTION getName(ip_product_id NUMBER,ip_company_id NUMBER)
  4    RETURN VARCHAR2
  5    IS
  6      v_name VARCHAR2(120);
  7    BEGIN
  8       SELECT 'Org Name: (Short) '||company_short_name||' (Long) '||company_long_name
  9       INTO v_name
 10       FROM company
 11       WHERE product_id = ip_product_id
 12       AND company_id = ip_company_id;
 13       RETURN (v_name);
 14    END getName;
 15  END myPackage;
 16  /

Package body created.

SQL>
SQL> -- Calling the above packaged function from SQL
SQL> SELECT myPackage.getName(product_id,company_id) "Formatted Org Name"
  2  FROM company
  3  ORDER BY product_id,company_id;

Formatted Org Name
----------------------------------------------------------------------
Org Name: (Short) A Inc. (Long) Long Name A Inc.
Org Name: (Short) B Inc. (Long) Long Name B Inc.
Org Name: (Short) C Inc. (Long) Long Name C Inc.
Org Name: (Short) D Inc. (Long) Long Name D Inc.
Org Name: (Short) E Inc. (Long) Long Name E Inc.
Org Name: (Short) F Inc. (Long) Long Name F Inc.

6 rows selected.

SQL>
SQL>
SQL> drop table company;

Table dropped.

SQL>








27.11.Package Body
27.11.1.Package with only one function
27.11.2.Package with two procedures
27.11.3.Package declaration and body
27.11.4.Use of 'get' and 'set' prefixes
27.11.5.Use package method in a procedure
27.11.6.Call function in a Package
27.11.7.Method overload
27.11.8.Package method overloading
27.11.9.Reference method from another package
27.11.10.Package Function with 'PRAGMA AUTONOMOUS_TRANSACTION'
27.11.11.Create a package containing stored procedure DELETE_ORDERS and stored function GET_employee_NAME.
27.11.12.Package initialization.
27.11.13.Use package member variable to pass value