Using stored functions in SQL statements, function getName : Create Procedure « Function Procedure Packages « Oracle PL/SQL Tutorial






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

Function created.

SQL>
SQL> SELECT 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> drop table company;

Table dropped.

SQL>








27.6.Create Procedure
27.6.1.Creating a procedure
27.6.2.Creating a Stored Procedure for table update
27.6.3.Call a trigger in procedure
27.6.4.Re-creating a Procedure By Using OR REPLACE
27.6.5.Exceptions in Subprograms
27.6.6.Forward Declarations
27.6.7.Using stored functions in SQL statements, function getName
27.6.8.Create procedure for AUTHID CURRENT_USER