Sunteți pe pagina 1din 20

Stored Procedure

Trigger
Introduction
 A Procedure or Function is a logically grouped set of SQL
and PL/SQL statements that performs a specific task.
 A stored procedure or stored function is a named PL/SQL
code block that has been compiled and stored in one of the
Oracle engine’s system tables.
Introduction
 Procedures and functions are stored in Oracle database and can be
called by any PL/SQL block.
 While creating a procedure or function, Oracle engine performs
following steps:
 Compiles the procedure or function.
 Stores procedure or function in database.
 During compilation, if error occurs then an invalid procedure or
function is created. To view the errors,
 SELECT * FROM USER_ERRORS;
 When procedure or function is invoked, Oracle engine loads the
compiled procedure or function in a memory area called System
Global Area (SGA).
 To execute the procedure or function, Oracle engine
performs following task:
 Verifies user access
 Verifies procedure or function validity
 Executes procedure or function
 To verify the validity of procedure or function following
select statement is used:
SELECT <ObjectName>, <ObjectType>, <Status> FROM
<UserObjects> where <ObjectType>=‘PROCEDURE’;
Advantages
 Security
 Performance
 Memory Allocation
 Productivity
 Integrity
Difference between
Procedure and Function
 A function must return a value back to caller. A function can
return only one value to the calling PL/SQL code block.
 By defining multiple OUT parameters in a procedure,
multiple values can be passed to the caller.
Parts of Procedure
 Declarative part: It can declare cursors, constants,
variables, exceptions and subprograms.
 Executable part: It contains SQL and PL/SQL statements.
 Exception-handling part (Optional): It deals with
exceptions that may be raised during the execution of code in
executable part.
Stored Procedure
CREATE OR REPLACE PROCEDURE [Schema.]
<ProcedureName>
(<Argument> {IN, OUT, IN OUT} <data_type>,…)
{IS,AS}
<variable> <datatype>;
BEGIN
<PL/SQL body>
EXCEPTION
<Exception PL/SQL block>
END:
Example
CREATE OR REPLACE Procedure UpdateE( name_in IN varchar2 )
IS
cnumber number; cursor c1 is SELECT EMPNO FROM EMP WHERE Ename =
name_in;
BEGIN
open c1; fetch c1 into cnumber;
if c1%notfound then
cnumber := 9999;
INSERT INTO EMP ( ENAME, EMPNO )VALUES ( name_in, cnumber );
DBMS_OUTPUT.PUT_LINE('INSERTED');
ELSE
DBMS_OUTPUT.PUT_LINE(cnumber);
end if; close c1;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -
ERROR- '||SQLERRM);
END;
Call Procedure
 Example:
 If name is not present in emp table then it is inserted
BEGIN
UpdateE('ND');
END;

 If name is not present in emp table then its empno is


displayed.
BEGIN
UpdateE(‘KING');
END;
Trigger
 Triggers are stored programs, which are automatically executed or
fired when some events occur. Triggers are, in fact, written to be
executed in response to any of the following events −
 A database manipulation (DML) statement (DELETE,
INSERT, or UPDATE)
 A database definition (DDL) statement (CREATE, ALTER, or
DROP).
 A database operation (SERVERERROR, LOGON, LOGOFF,
STARTUP, or SHUTDOWN).
 Triggers can be defined on the table, view, schema, or database
with which the event is associated.
Types of Trigger
 Row Level Triggers
 Statement Level Triggers
 Before Triggers
 After Triggers
 Combination Trigger
Types of Trigger
 BEFORE Trigger:
 BEFORE trigger execute before the triggering DML statement
(INSERT, UPDATE, DELETE) execute. Triggering SQL
statement is may or may not execute, depending on the
BEFORE trigger conditions block.
 AFTER Trigger:
 AFTER trigger execute after the triggering DML statement
(INSERT, UPDATE, DELETE) execute. Triggering SQL
statement is execute as soon as followed by the code of trigger
before performing Database operation.
Types of Trigger
 ROW Trigger:
 ROW trigger fire for each and every record which are
performing INSERT, UPDATE and DELETE from the database
table. If row deleting is define as trigger event, when trigger
file, deletes the five rows each times from the table.
 STATEMENTTrigger:
 STATEMENT trigger fire only once for each statement. If row
deleting is define as trigger event, when trigger file, deletes the
five rows at once from the table.
Types of Trigger
 Combination Trigger: Combination trigger are combination
of two trigger type,
 BEFORE STATEMENTTrigger:
 Trigger fire only once for each statement before the triggering
DML statement.
 BEFORE ROW Trigger:
 Trigger fire only once for each and every record before the
triggering DML statement.
 AFTER STATEMENTTrigger:
 Trigger fire only once for each statement after the triggering DML
statement executing.
 AFTER ROW Trigger:
 Trigger fire only once for each and every record after the
triggering DML statement executing.
Dropping Triggers
 Triggers may be dropped via the drop trigger command. In
order to drop a trigger, you must either own the trigger or
have the DROP ANY TRIGGER system privilege.

 Syntax: DROP TRIGGER trigger_name;


Syntax
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name]
ON table_name [REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW] WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
Raise_Application_Error
 The raise_application_error is actually a procedure defined by
Oracle that allows the developer to raise an exception and
associate an error number and message with the procedure.
 This allows the application to raise application errors rather
than just Oracle errors.
 Error numbers are defined between -20,000 and -20,999.
 Oracle provides the raise_application_error procedure to allow
you to raise custom error numbers within your applications.
 Syntax:
raise_application_error(Error NUmber, Message);
Example
Create or replace Trigger np
before insert or update on emp
for each row Begin
IF :NEW.sal <= 0 THEN
DBMS_OUTPUT.PUT_LINE('SALARY IS NAGATIVE..');
END IF;
End;
 Checking Trigger
insert into emp(empno,sal)values(1,0);
Example
Create or replace Trigger np
before insert or update on emp
for each row Begin
IF :NEW.sal <= 0 THEN
Raise_application_error(-20001,'SALARY IS NAGATIVE..');
END IF;
End;
 Checking Trigger
insert into emp(empno,sal)values(1,0);

S-ar putea să vă placă și