Les09 Creating Procedures
Les09 Creating Procedures
Les09 Creating Procedures
Creating Procedures
BEGIN
EXCEPTION
Database Server
Tools Constructs Constructs
Anonymous blocks END;
Anonymous blocks
Application procedures or
Stored procedures or
functions
functions
Application packages
Stored packages
Application triggers
Database triggers
Object types
Object types
A subprogram:
• Is a named PL/SQL block that can accept parameters
and be invoked from a calling environment
• Is of two types:
– A procedure that performs an action
– A function that computes a value
• Is based on standard PL/SQL block structure
• Provides modularity, reusability, extensibility,
and maintainability
• Provides easy maintenance, improved data security
and integrity, improved performance, and improved
code clarity
DECLARE (optional)
Declare PL/SQL objects to be used
within this block
BEGIN (mandatory)
Define the executable statements
EXCEPTION (optional)
Define the actions that take place if
an error or exception arises
END; (mandatory)
<header>
Subprogram Specification
IS | AS
Declaration section
BEGIN
Executable section
Subprogram Body
EXCEPTION (optional)
Exception section
END;
• Easy maintenance
• Improved data security and integrity
• Improved performance
• Improved code clarity
2 3
iSQL*Plus
2 Load and execute file.sql
•• Formal
Formal parameters:
parameters: variables
variables declared
declared in
in the
the
parameter
parameter list
list of
of aa subprogram
subprogram specification
specification
Example:
Example:
CREATE
CREATE PROCEDURE
PROCEDURE raise_sal(p_id
raise_sal(p_id NUMBER,
NUMBER, p_amount
p_amount NUMBER)
NUMBER)
...
...
END
END raise_sal;
raise_sal;
•• Actual
Actual parameters:
parameters: variables
variables or
or expressions
expressions
referenced
referenced in
in the
the parameter
parameter list
list of
of aa subprogram
subprogram call
call
Example:
Example:
raise_sal(v_id,
raise_sal(v_id, 2000)
2000)
Procedure
IN parameter
Calling
environment OUT parameter
IN OUT parameter
(DECLARE)
BEGIN
EXCEPTION
END;
176 p_id
171 p_id
SMITH p_name
7400 p_salary
0.15 p_comm
emp_query.sql
CREATE OR REPLACE PROCEDURE query_emp
(p_id IN employees.employee_id%TYPE,
p_name OUT employees.last_name%TYPE,
p_salary OUT employees.salary%TYPE,
p_comm OUT employees.commission_pct%TYPE)
IS
BEGIN
SELECT last_name, salary, commission_pct
INTO p_name, p_salary, p_comm
FROM employees
WHERE employee_id = p_id;
END query_emp;
/
PRINT g_name
leave_emp2.sql
CREATE OR REPLACE PROCEDURE leave_emp2
(p_id IN employees.employee_id%TYPE)
IS
PROCEDURE log_exec
IS
BEGIN
INSERT INTO log_table (user_id, log_date)
VALUES (USER, SYSDATE);
END log_exec;
BEGIN
DELETE FROM employees
WHERE employee_id = p_id;
log_exec;
END leave_emp2;
/
DECLARE
v_id NUMBER := 163;
BEGIN
raise_salary(v_id); --invoke procedure
COMMIT;
...
END;
process_emps.sql
CREATE OR REPLACE PROCEDURE process_emps
IS
CURSOR emp_cursor IS
SELECT employee_id
FROM employees;
BEGIN
FOR emp_rec IN emp_cursor
LOOP
raise_salary(emp_rec.employee_id);
END LOOP;
COMMIT;
END process_emps;
/
Called procedure
Calling procedure PROCEDURE
PROC2 ...
PROCEDURE IS
PROC1 ... ...
IS BEGIN
... ... Exception raised
BEGIN EXCEPTION
... ... Exception handled
PROC2(arg1); END PROC2;
...
EXCEPTION Control returns to
...
END PROC1; calling procedure
Called procedure
Calling procedure
PROCEDURE
PROCEDURE PROC2 ...
PROC1 ... IS
IS ...
... BEGIN
... Exception raised
BEGIN
... EXCEPTION
... Exception unhandled
PROC2(arg1); END PROC2;
...
EXCEPTION
...
END PROC1; Control returned to
exception section of
calling procedure
Drop
Drop aa procedure
procedure stored
stored in
in the
the database.
database.
Syntax:
Syntax:
DROP PROCEDURE procedure_name
Example:
Example:
DROP PROCEDURE raise_salary;