Professional Documents
Culture Documents
Stored Procedure
A stored procedure is a named PL/SQL block that
performs an action. It can be stored in the database
as a database object for repeated execution.
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
Parameters Modes in Procedures and Functions
Formal parameters can have three modes IN, OUT or IN OUT which decides
the behavior of parameters
IN OUT IN OUT
Note: If no parameters are specified for a procedure, directly specify procedure name
without any parenthesis
e. g. EXECUTE procedure_name;
OUT Parameter: Example
8
Example 1
declare
p_sal emp.sal%TYPE;
p_empno emp.empno%TYPE := &empno;
begin
p_sal := cal_bonus(p_empno);
update emp set sal = sal + p_sal where empno = p_empno;
commit;
--dbms_output.put_line(p_sal);
end;
/
Functions in PL/SQL
15
Example 2
Procedures Functions
Do not contain RETURN clause in the Must contain a RETURN clause in the
header header
Can return none, one or many values with Must return a value using the RETURN
the help of OUT & IN OUT parameters clause. Can also return one or many values
with the help of OUT and IN OUT
parameters.
1
6
Dropping PROCEDURES and FUNCTIONS
17
To Delete a PROCEDURE:
To Delete a FUNCTION:
USER_SOURCE
Is used to obtain the text of a stored procedure or a stored
function
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 user’s
schema
20
DML TRIGGERS
Database Triggers
Triggers are named PL/SQL blocks with
declarative, executable, and exception handling
sections
A trigger is executed implicitly whenever the
triggering event takes place
Triggers do not accept arguments
Like packages, triggers are stored database
objects and can not be local to a block
21
Stored Procedures vs Database
Triggers
2
2
DML Triggers
Application:
To impose complex integrity constraints not
possible through declarative constraints
To audit information in a table
To create replica of a table etc.
Security reasons
23
DML Triggers
DML Trigger Components
Part Description Possible Values
Syntax:
25
Statement Triggers: Example
26
Row Triggers
The trigger body executes once for each row
affected by the triggering event.
Syntax for creating Row Triggers:
CREATE [ OR REPLACE ] TRIGGER trigger_name
trigger_timing event1 [ OR event2 OR event3 ]
ON table_name
FOR EACH ROW
[WHEN condition]
PL/SQL Block;
FOR EACH ROW: Designates the trigger to be a row trigger
WHEN condition: Specifies the trigger restriction
For the UPDATE event there is an optional clause
[OF column_name[,column_name…..]] 27
Using OLD and NEW Qualifiers
In a ROW LEVEL trigger all the column values of the current row, before
modification and after modification, are available to the trigger block as
local variables. To access these values, the OLD and NEW quantifiers are
used
28
:old and :new variables:
Example
CREATE TABLE emp_hist
( empno NUMBER(5),
oldSal NUMBER(10),
newSal NUMBER(10));
30
WHEN Clause
32
Managing Triggers
Disable / Enable trigger:
ALTER TRIGGER trigger_name ENABLE/DISABLE;
Dropping Triggers:
DROP TRIGGER trigger_name;
33
34
THANK YOU