330 likes | 338 Views
Creating Functions. 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 database object, for repeated execution. A function can be called as part of an expression. Syntax for Creating Functions.
E N D
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 database object, for repeated execution. • A function can be called as part of an expression.
Syntax for Creating Functions CREATE [OR REPLACE] FUNCTION function_name (argument1 [mode1] datatype1, argument2 [mode2] datatype2, . . . RETURN datatype IS|AS PL/SQL Block;
Creating a Stored Function Using SQL*Plus • 1. Enter the text of the CREATE FUNCTION statement in a system editor or word processor and save it as a script file (.sql extension). • 2. From SQL*Plus, run the script file to compile the source code into p-code and store both in the database. 3. Invoke the function from an Oracle Server environment to determine whether it executes without error.
Creating a Stored Function Using SQL*Plus: Example SQL> CREATE OR REPLACE FUNCTION get_sal 2 (v_id IN emp.empno%TYPE) 3 RETURN NUMBER 4 IS 5 v_salary emp.sal%TYPE :=0; 6 BEGIN 7 SELECT sal 8 INTO v_salary 9 FROM emp 10 WHERE empno = v_id; 11 RETURN (v_salary); 12 END get_sal; 13 /
Executing Functions • Invoke a function as part of a PL/SQL expression. • Create a host variable to hold the returned value. • Execute the function. The host variable will be populated by the RETURN value.
Advantages of User-Defined Functions in SQL Expressions • Extend SQL where activities are too complex, too awkward, or unavailable with SQL • Query efficiency: functions used in the WHERE clause can filter data • Manipulate character strings • Provide parallel query execution
Removing Functions • Using SQL*Plus: Drop a server-side function. • Using Procedure Builder: • Drop a server-side function. • Drop a client-side function.
Removing a Server-SideFunction • Using SQL*Plus • Syntax • Example DROP FUNCTION function_name SQL> DROP FUNCTION get_salary; Function dropped.
Function Procedure IN argument IN argument OUT argument IN OUT argument (DECLARE) BEGIN EXCEPTION END; (DECLARE) BEGIN EXCEPTION END; Procedure or Function? Calling Environment Calling Environment
Comparing Procedures and Functions Procedure Execute as a PL/SQL statement No RETURN datatype Can return one or more values Function Invoke as part of an expression Must contain a RETURN datatype Must return a value
Benefits of Stored Procedures and Functions • Improved performance • Improved maintenance • Improved data security and integrity
Overview of Packages • Group logically related PL/SQL types, items, and subprograms • Consist of two parts: • Specification • Body • Cannot be called, parameterized, or nested • Allow Oracle8 to read multiple objects into memory at once
Advantages of Packages • Modularity • Easier application design • Information hiding • Added functionality • Better performance • Overloading
Developing a Package 1 Package specification Procedure Adeclaration 2 4 Procedure Bdefinition 3 Package body Procedure A definition 2 5
Developing a Package 1 Package specification Procedure Adeclaration 2 4 Procedure Bdefinition 3 Package body Procedure A definition 2 5
Developing a Package Procedure Builder Edit Textfile SystemEditor 1 Store in database Sourcecode 2 Compile p-code Invoke
Developing a Package • Saving the text of the CREATE PACKAGE statement in two different text files facilitates later modifications to the package. • A package specification can exist without a package body, but a package body cannot exist without a package specification. • If you have incorporated a standalone procedure into a package, you should drop your standalone procedure.
Creating the Package Specification Syntax CREATE [OR REPLACE] PACKAGE package_name IS | AS public type and item declarations subprogram specifications END package_name;
Declaring Public Constructs COMM_PACKAGE package G_COMM 1 Package specification RESET_COMMproceduredeclaration 2
Creating a Package Specification: Example SQL>CREATE OR REPLACE PACKAGE comm_package IS 2 g_comm NUMBER := 10; --initialized to 10 3 PROCEDURE reset_comm 4 (v_comm IN NUMBER); 5 END comm_package; 6 /
Declaring a Global Variable or a Public Procedure SQL> EXECUTE comm_package.g_comm := 5 SQL> EXECUTE comm_package.reset_comm(8)
Creating the Package Body Syntax CREATE [OR REPLACE] PACKAGE BODY package_name IS | AS private type and item declarations subprogram bodies END package_name;
Public and Private Constructs COMM_PACKAGE package 1 G_COMM Package specification RESET_COMM procedure declaration 2 VALIDATE_COMMfunction definition 3 Package body RESET_COMMprocedure definition 2
Creating a Package Body: Example SQL>CREATE OR REPLACE PACKAGE BODY comm_package IS 2 FUNCTION validate_comm 3 (v_comm IN NUMBER) RETURN BOOLEAN 4 IS 5 v_max_comm NUMBER; 6 BEGIN 7 SELECT MAX(comm) 8 INTO v_max_comm 9 FROM emp; 10 IF v_comm > v_max_comm THEN RETURN(FALSE); 11 ELSE RETURN(TRUE); 12 END IF; 13 END validate_comm; 14 END comm_package; 15 /
Creating a Package Body: Example SQL>PROCEDURE reset_comm 2 (v_comm IN NUMBER) 3 IS 4 v_valid BOOLEAN; 5 BEGIN 6 v_valid := validate_comm(v_comm); 7 IF v_valid = TRUE THEN 8 g_comm := v_comm; 9 ELSE 10 RAISE_APPLICATION_ERROR 11 (-20210,'Invalid commission'); 12 END IF; 13 END reset_comm; 14 END comm_package; 15 /
Invoking Package Constructs • Example 1: Invoke a function from a procedure within the same package. CREATE OR REPLACE PACKAGE BODY comm_package IS . . . PROCEDURE reset_comm(v_comm IN NUMBER) IS v_valid BOOLEAN; BEGIN v_valid := validate_comm(v_comm); IF v_valid = TRUE THEN g_comm := v_comm; ELSE RAISE_APPLICATION_ERROR (-20210, 'Invalid comm'); END IF; END reset_comm; END comm_package;
Invoking Package Constructs • Example 2: Invoke a package procedure from SQL*Plus. • Example 3: Invoke a package procedure in a different schema. • Example 4: Invoke a package procedure in a remote database. SQL> EXECUTE comm_package.reset_comm(1500); SQL> EXECUTE scott.comm_package.reset_comm(1500); SQL> EXECUTE comm_package.reset_comm@ny (1500);
Referencing a Global Variable Within the Package Example 1 CREATE OR REPLACE PACKAGE BODY comm_package IS . . . PROCEDURE reset_comm(v_comm IN NUMBER) IS v_valid BOOLEAN; BEGIN v_valid := validate_comm(v_comm); IF v_valid = TRUE THEN g_comm := v_comm; ELSE RAISE_APPLICATION_ERROR (-20210,'Invalid comm'); END IF; END reset_comm; END comm_package;
Removing Packages To remove the package specification and the body: DROP PACKAGE package_name To remove the package body: DROP PACKAGE BODY package_name
Summary • A function is a named PL/SQL block that must return a value. • A function is invoked as part of an expression. • A stored function can be called in SQL statements.
Summary • Improve organization, management, security, and performance. • Group related procedures and functions together. • Change a package body without affecting a package specification. • Grant security access to the entire package.