Objectives After completing this lesson, you should be able to do the following: Describe the uses of functions Create client-side and server-side functions Creating Functions Invoke a function Remove a function Differentiate between a procedure and a function Overview of Stored Functions A function is a named PL/SQL block that returns a value. A function can be stored in the database, as a schema object, for repeated execution. Syntax for Creating Functions CREATE [OR REPLACE] FUNCTION function_name [(parameter1 [mode1] datatype1, parameter2 [mode2] datatype2, . . .)] RETURN datatype IS|AS PL/SQL Block; A function can be called as part of an expression. The PL/SQL block must have at least one RETURN statement. Creating a Function Oracle Procedure Builder 1 Editor Code Save SQL*Plus 2 Creating a Stored Function by Using SQL*Plus Code SQL> START file.sql 1. Enter the text of the CREATE FUNCTION statement in an editor. 2. Run the script file to store the source code and compile the function. 3. Use SHOW ERRORS to see compilation errors. Oracle Source code Compile 4. When successfully compiled, the function is ready for execution. P code Execute <Course name> <Lesson number>- Creating a Stored Function by Using SQL*Plus: Example SQL> 2 3 4 5 6 7 8 9 10 11 12 13 Invoke a function as part of a PL/SQL expression. CREATE OR REPLACE FUNCTION get_sal (v_id IN emp.empno%TYPE) RETURN NUMBER IS v_salary emp.sal%TYPE :=0; BEGIN SELECT sal INTO v_salary FROM emp WHERE empno = v_id; RETURN v_salary; END get_sal; / Create a host variable to hold the returned value. Execute the function. The host variable will be populated by the RETURN value. Executing Functions in SQL*Plus: Example Calling environment GET_SAL function v_id 7934 Executing Functions RETURN v_salary Creating a Function by Using Procedure Builder Procedure Builder allows you to: Create a client-side function Create a server-side function Drag and drop functions between client and server SQL> START get_salary.sql Function created. SQL> VARIABLE g_salary number SQL> EXECUTE :g_salary := get_sal(7934) PL/SQL procedure successfully completed. SQL> PRINT g_salary G_SALARY -----------------1300 Creating Functions by Using Procedure Builder: Example Return the tax based on a specified value. FUNCTION tax (v_value IN NUMBER) RETURN NUMBER IS BEGIN RETURN (v_value * .08); END tax; Executing Functions in Procedure Builder: Example Calling environment TAX function v_value 1000 RETURN (computed value) Display the tax based on a specified value. PL/SQL> .CREATE NUMBER x PRECISION 4 PL/SQL> :x := tax(1000); PL/SQL> TEXT_IO.PUT_LINE (TO_CHAR(:x)); 80 <Course name> <Lesson number>- Advantages of User-Defined Functions in SQL Expressions Extend SQL where activities are too complex, too awkward, or unavailable with SQL Can increase efficiency, by using them in the WHERE clause to filter data, as opposed to filtering the data in the application Can manipulate character strings Locations to Call User-Defined Functions Invoking Functions in SQL Expressions: Example SQL> CREATE OR REPLACE FUNCTION tax 2 (v_value IN NUMBER) 3 RETURN NUMBER 4 IS 5 BEGIN 6 RETURN (v_value * .08); 7 END tax; 8 / Function created. SQL> SELECT empno, ename, sal, tax(sal) 2 FROM emp; Calling Functions from SQL Expressions: Restrictions A user-defined function must be a stored function. Select list of a SELECT command Condition of the WHERE and HAVING clauses A user-defined function must be a SINGLE-ROW function, not a GROUP function. CONNECT BY, START WITH, ORDER BY, and GROUP BY clauses A user-defined function only takes IN parameters, not OUT or IN OUT. VALUES clauses of the INSERT command Data types must be valid SQL datatypes, CHAR, DATE, or NUMBER. SET clause of the UPDATE command Data types cannot be PL/SQL types such as BOOLEAN, RECORD, or TABLE. Calling Functions from SQL Expressions: Restrictions Removing Functions INSERT, UPDATE, or DELETE commands are not allowed. Using SQL*Plus: Calls to subprograms that break the above restriction are not allowed. Using Procedure Builder: Drop a server-side function Drop a server-side function Delete a client-side function <Course name> <Lesson number>- Removing Server-Side Functions Removing a Server-Side Function Using Procedure Builder: Using SQL*Plus: 1. Connect to the database. 2. Expand the Database Objects node. Syntax 3. Expand the schema of the owner of the function. DROP FUNCTION function_name 4. Expand the Stored Program Units node. 5. Click the function that you want to drop. Example 6. Click Delete in the Object Navigator. SQL> DROP FUNCTION get_sal; Function dropped. 7. Click Yes to confirm. Removing a Client-Side Function Procedure or Function? Function Procedure Using Procedure Builder: Calling Environment 1. Expand the Program Units node. IN parameter OUT parameter Calling Environment IN parameter 2. Click the function that you want to remove. IN OUT parameter 3. Click Delete in the Object Navigator. (DECLARE) (DECLARE) 4. Click Yes to confirm. BEGIN BEGIN EXCEPTION EXCEPTION END; END; Comparing Procedures and Functions Procedure Function Execute as a PL/SQL statement Invoke as part of an expression No RETURN data type Must contain a RETURN data type Can return none, one or many values Must return a single value <Course name> <Lesson number>- This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.