Pls QL 4 Sub Programs
Pls QL 4 Sub Programs
Pls QL 4 Sub Programs
Objectives
At the end of this session, you will be able to:
Understand subprograms and their need Comprehend the difference between stored and unstored subprograms Create and drop stored procedures and functions Apply different parameter modes in subprograms Understand and use procedures and functions that are local to a stored subprogram Understand the different data dictionary views
Agenda
Need for subprograms Creating Stored Procedures RAISE_APPLICATION_ERROR Parameter Modes IN, OUT and IN OUT Invoking stored procedures from SQL*Plus and PL/SQL Block Examples of stored procedures and their invocation Creating Stored Functions Calling Stored Functions from different locations in DML and SELECT Queries Local Procedures and Functions within a stored subprogram Parameter passing mechanisms Privileges related to functions and procedures Dropping procedures and functions Data dictionary views related to subprograms
USER_OBJECTS USER_SOURCE USER_PROCEDURES USER_ERRORS
Subprograms in PL/SQL
PL/SQL blocks are anonymous, can not be called from other PL/SQL blocks Subprogram is a named PL/SQL block that could be either
Stored Procedure, or Stored Function, or Package
CREATE [ OR REPLACE ] PROCEDURE proc_name[(argument [mode] data type, argument [mode] data type,)]
-- PROCEDURES and FUNCTIONS may or may not accept arguments IS Local PL/SQL variable declarations BEGIN Define action performed by the procedure EXCEPTION Handle exceptions, if any END [ proc_name]; /
RAISE_APPLICATION_ERROR Procedure
This procedure is used to display error messages along with error numbers Example RAISE_APPLICATION_ERROR(-20001, Invalid Employee); Note:
When called, RAISE_APPLICATION_ERROR ends a subprogram, rolls back any database changes it made, and returns a user-defined error message to the application Error numbers should be between -20000 and -20999
Formal parameter acts as a constant inside a subprogram Actual parameter can be an expression, constant or initialized variable
Formal parameter acts as a uninitialized variable, as the subprogram will assign value to it Actual parameter must be a variable
IN Parameter: Example
CREATE OR REPLACE PROCEDURE raise_salary (p_eno emp.empno%TYPE) IS vsal emp.sal%type; BEGIN SELECT sal into vsal FROM emp WHERE empno=p_eno; IF vsal <2000 THEN UPDATE emp SET sal = sal+vsal * 0.1 WHERE empno = p_eno; END IF; -- An exception handler to raise error if empno is not valid EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE (Empno does not exist); END raise_salary; /
Invoking a Procedure
There are two methods to Invoke a Procedure
Invoke a Procedure from another PL/SQL block e.g. BEGIN raise_salary(1002); END; / Invoke a procedure from SQL*Plus environment by using EXECUTE command e.g. -- PROCEDURE call with parameter EXECUTE raise_salary(1002); Note: If no parameters are specified for a procedure, directly specify procedure name without any parenthesis
e. g.
EXECUTE procedure_name;
10
Note: The use of colon (:) is to reference the host variable in the EXECUTE syntax
11
12
13
14
Functions in PL/SQL
Example 1 CREATE OR REPLACE FUNCTION cal_bonus ( p_eno emp.empno%TYPE ) RETURN NUMBER IS v_sal emp.sal%TYPE; BEGIN SELECT sal INTO v_sal FROM WHERE empno = p_eno; RETURN (v_sal * 0.1); EXCEPTION WHEN NO_DATA_FOUND THEN RETURN -1; END;
emp
15
Functions in PL/SQL
Example 2
CREATE OR REPLACE FUNCTION chk_dept (p_deptno dept.deptno%TYPE) RETURN BOOLEAN IS v_deptno dept.deptno%type; BEGIN SELECT deptno into v_deptno FROM dept WHERE deptno = p_deptno; RETURN TRUE; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN FALSE; END; /
16
Note: Not all functions can be called from above mentioned locations.
17
Rules for Calling a Function through a SQL Statement To be callable from SQL statement:
The function must be stored in database as either stand alone function or as a part of a package The function can take only IN type of parameters The formal parameters and return type of the function must be oracle data types The function must not end the current transaction (commit or rollback) or rollback to a savepoint prior to the function execution The function must not issue any alter session or alter system commands
18
Functions
Invoked as part of an expression
Can return none, one or many values with the help of OUT & IN OUT parameters
Must return a value using the RETURN clause. Can also return one or many values with the help of OUT and IN OUT parameters.
19
Note: The command will run successfully only if the user HR is granted SELECT right on Scott's emp table
To execute a procedure belonging to scotts schema the user HR will use the following command EXECUTE scott.procedure_name
20
21
22
23
24
25
26
Note: While using combination the positional parameters should be passed first
27
28
Note: All the positional parameters should precede the named parameters in a subprogram call.
29
USER_ERRORS
Is used to find out the compilation errors in the subprogram, currently getting compiled One can use the SQL*Plus command SHOW ERRORS, instead of firing a SELECT query on USER_ERRORS
USER_OBJECTS
Is used to get the details of all the objects created in a particular schema
USER_PROCEDURES
Is used to get the details of all procedures in that users schema
30
Summary
In this session, we have covered:
Need for subprograms Creating Stored Procedures Invoking stored procedures from SQL*Plus and PL/SQL Block Subprogram Parameter Modes IN, OUT and IN OUT Creating Stored Functions Calling Stored Functions from different locations Privileges related to functions and procedures Dropping procedures and functions Local Procedures and Functions within a stored subprogram Methods of passing Parameters Data dictionary views related to subprograms
USER_OBJECTS USER_SOURCE USER_PROCEDURES USER_ERRORS
31
Thank You