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>