5.20 Local functions

 < Day Day Up > 



5.20 Local functions

Oracle allows procedures or functions - called Local Procedures or Local Functions - to be created and referenced within the body of a Stored Procedure. When converting this to DB2, the Local Procedure or Local Function must be created outside of the Stored Procedure that references it, before it can be used. The example below shows how a Local Function created and referenced in an Oracle Stored procedure would be converted to DB2.

Example 5-42 shows the Oracle source code of the function.

Example 5-42: Oracle procedure with Local function

start example
 CREATE OR REPLACE PROCEDURE callLocalFunc AS   /* Local declarations, which include a cursor, variable, and a      function. */   CURSOR c_AllEmployees IS     SELECT first_name, last_name       FROM employees;   v_FormattedName VARCHAR2(50);   /* Function which will return the first and last name      concatenated together, separated by a space. */   FUNCTION FormatName(p_FirstName IN VARCHAR2,                       p_LastName IN VARCHAR2)     RETURN VARCHAR2 IS   BEGIN     RETURN p_FirstName || ' ' || p_LastName;   END FormatName; -- Begin main block. BEGIN   FOR v_EmployeeRecord IN c_AllEmployees LOOP     v_FormattedName :=       FormatName(v_ EmployeeRecord.first_name,                  v_ EmployeeRecord.last_name);     DBMS_OUTPUT.PUT_LINE(v_FormattedName);   END LOOP; END callLocalFunc; 
end example

Example 5-43 shows the DB2 conversion of the function.

Listing 5-43: DB2 Conversion of Oracle Procedure with Local Function

start example
 CREATE FUNCTION FormatName(p_FirstName VARCHAR(20),        --[1]                            p_LastName VARCHAR(20))     RETURNS VARCHAR(41) LANGUAGE SQL BEGIN ATOMIC    RETURN p_FirstName || '' || p_LastName; END! CREATE PROCEDURE callFunc () LANGUAGE SQL BEGIN  DECLARE c_AllEmployees CURSOR WITH RETURN FOR    SELECT FormatName(first_name, last_name) FROM employees;  OPEN c_AllEmployees; END! 
end example

Notes

[1] 

The function must be created before the procedure that references it.



 < Day Day Up > 



Oracle to DB2 UDB Conversion Guide2003
Oracle to DB2 UDB Conversion Guide2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net