Procedures and Triggers1

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Procedures and triggers

Procedures and triggers


Objectives

 Discuss PL/SQL
 Discuss
procedures
 Create a
procedure
 Discuss trigger
 Create a trigger
What is PL/SQL and what it can do?
PL/SQL program block
Declaration section :- necessary variables are declared
in this section
Begin : this part of block consist of SQL and PL/SQL
statements that assign values,controle execution and
manipulate data
Exception :this part contains code that deals with
exceptions that may be raised during the execution of
code in the executable part
EXAMPLE

DECLARE
A NUMBER(6);
B NUMBER(6);
BEGIN
A := 23;
B := A * 5;
IF A < B THEN
DBMS_OUTPUT.PUT_LINE(’Ans: ’ || A || ’ is less
than ’ || B);
END IF;
END;

Which produces the expected output of:


Ans: 23 is less than 115
PL/SQL –PROCEDURES
A stored procedure or in simple a proc is a named
PL/SQL block which performs one or more specific task.
This is similar to a procedure in other programming
languages.
A procedure has a header and a body. The header
consists of the name of the procedure and the parameters
or variables passed to the procedure.
The body consists or declaration section, execution
section and exception section similar to a general
PL/SQL Block.
 A procedure is similar to an anonymous PL/SQL Block
but it is named for repeated usage.
Syntax -
CREATE OR REPLACE PROCEDURE [SCHEMA]
<procedure name>(<argument> {IN,OUT,IN OUT} <data
type>,…..){ IS ,AS}
<variable> declarations;
<constant> declarations;
BEGIN
<PL/SQL sub program body>;
EXCEPTION
<EXCEPTION PL/SQL BLOCK>;
END
SYNTAX FOR DELETION –
DROP<procedure name>;
CREATE OR REPLACE
explanation
PROCEDURE
[SCHEMA] <procedure
name>(<argument>  REPLACE –recreates the
{IN,OUT,IN OUT} <data procedure if it already exist
type>,…..){ IS ,AS}  SCHEMA – is the schema to
<variable> declarations; contain procedure
<constant> declarations;  PROCEDURE – is the name
BEGIN of procedure to be created
 ARGUMENT –Is the name of
<PL/SQL sub program
body>; argument to the procedure
 IN – indicate the value will be
EXCEPTION
accepted from user
<EXCEPTION PL/SQL  OUT- indicate the parameter
BLOCK>;
will return value to the user
END  IN OUT- indicate that
parameter will either accept the
value from the user or return
the value to the user
example
CREATE OR REPLACE PROCEDURE hello IS
Greetings VARCHAR(20);
BEGIN
Greetings:= 'Hello World';
DBMS_OUTPUT.PUT_LINE(greetings);
END hello;
PL/SQL -TRIGGERS
 A trigger is a set of actions that are run automatically when
a specified change operation is performed on a specified
table.
 The change operation can be an SQL INSERT, UPDATE,
or DELETE statement in an application program.
 Triggers are fired implicitly
Types of triggers
 Row trigger
 Statement trigger
 Before trigger
 After trigger

ROW TRIGGER :-
This trigger is fired each time a row in in the table is affercted
by triggering statement
STATEMENT TRIGGER:-
Statement trigger is fired once on behalf of the triggering
statement independent of number of rows the triggering
staement effect
 BEFORE TRIGGER:-
before trigger executes trigger action before triggering statement
 it is used when trigger action determine wether or not
triggering statement should be allowed to complete.
 by ussing before trigger user can eleminate
unecessary processing of trigger statement
 AFTER TRIGGER:-
after trigger executes the trigger action after triggering
statement is executed
 if before trigger is present after trigger can perform different
action on same trigger
Syntax for creating trigger
 CREATE OR REPLACE TRIGGER[schema] <trigger
name>{before,after}{DELETE,INSERT,UPDATE[ of
column,….]} ON [schema] <table name>[REFERENCING
{old as old, New as new}][for each row [when condition]]
DECLARE
<variable declaration>;
<constant declaration>;
BEGIN
<PL/SQL subprogram body>
EXCEPTION
<Exception PL/SQL block>;
END;
 REPLACE –recreates the
procedure if it already exist Explanation
 SCHEMA – is the schema to
contain procedure  CREATE OR REPLACE
 TRIGGER NAME – Name of TRIGGER[schema] <trigger
trigger to be created name>{before,after}
{DELETE,INSERT,UPDATE[ of
 BEFORE – indicates trigger is fired column,….]} ON [schema] <table
before execution of trigger name>[REFERENCING {old as
 AFTER - indicates trigger is fired old, New as new}][for each row
after execution of trigger [when condition]]
 DELETE – indicate trigger is fired DECLARE
when DELETE statement removes
row from table <variable declaration>;
 INSERT - indicate trigger is fired <constant declaration>;
when INSERT statement removes BEGIN
row from table <PL/SQL subprogram body>
 UPDATE-indicate trigger if fires EXCEPTION
when UPDATE changes value in
one of the specified column in the <Exception PL/SQL block>;
OF clause. Else trigger is fired END;
whenever update statement is
executed
 CREATE OR REPLACE
 ON – specifies the schema
TRIGGER[schema] <trigger
and name of the table on name>{before,after}
which trigger is to be {DELETE,INSERT,UPDATE[
of column,….]} ON [schema]
executed <table name>[REFERENCING
 REFERENCING- specifies {old as old, New as new}][for
corelation name each row [when condition]]
DECLARE
 FOR EACH ROW-
<variable declaration>;
designates to be a trigger <constant declaration>;
row BEGIN
 WHEN – specifies the <PL/SQL subprogram
trigger restriction body>
 PL/SQL BLOCK- it is the EXCEPTION
block executed by database <Exception PL/SQL
block>;
wwhen trigger is fired END;
Example
 CREATE or REPLACE TRIGGER IncreaseDiscount
AFTER INSERT on Orders
FOR EACH ROW
BEGIN
UPDATE Customers SET discnt = discnt+.1
WHERE Customers.cid=:new.cid;
END;
/

Name of Trigger: IncreaseDiscount


Triggering Event: After insert on orders
Trigger Body or Trigger code that is fired: UPDATE customers …

:new and :old represent reserved words for Oracle’s PL/SQL. In this example,
:new represents the cid of the new row in the orders table that was just
inserted.
Deleting a trigger

Syntax – DROP TRIGGER<trigger name>;


Where trigger name is the name of
trigger to be deleted
Topics discussed
Introduction to PL/SQL
 Procedure
Triggers
ANY QUERIES

You might also like