0% found this document useful (0 votes)
22 views

PL SQL N

Uploaded by

sh.ashfaqueme49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

PL SQL N

Uploaded by

sh.ashfaqueme49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 7

TRIGGERS

Trigger are the event manager,they executed when an INSERT,UPDATE OR DELETE statement is
issued against the associated tables these procedures are called triggers.
Trigger can be defined only on tables not on views.
Types of Trigger
DML Trigger: it fires,when the DML event is specified (INSERT,UPDATE,DELETE).
DDL Trigger: it fires,when the DDL event is specified (CREATE,ALTER).
DATABASE Trigger: it fires,when the database event is specified.
(LOGON,LOGOFF,SHUTUP,SHUTDOWN).

CREATE [ OR REPLACE ] TRIGGER trigger_name


{BEFORE | AFTER}
{INSERT [OR] | UPDATE [OR] | DELETE}
ON table_name
[FOR EACH ROW]
DECLARE
Declaration-statement
BEGIN
Executable statement
END

EXAMPLE:-

create trigger salary_difference


before insert or delete or update
on emp_trigger
for each row
declare salary_difference number;
begin
salary_difference := :new.salary - :old.salary;
dbms_output.put_line('Old salary:'||:old.salary);
dbms_output.put_line('New salary:'||:new.salary);
dbms_output.put_line('Salary difference:'||salary_difference);
end;

#user defined error 20000 to 20999


#How to view triggers
>select trigger_name from user_triggers;
#How to view trigger body
>select trigger body from user_triggers where tigger_name=’salary_difference’;
#How to drop trigger.
>Drop trigger trigger_name.
#How to Disable/Enable triggers.
>Alter trigger trigger_name disable/enable.
INSTEAD OF TRIGGERS

CREATE [OR REPLACE] TRIGGER trigger_name


INSTEAD OF {INSERT | UPDATE | DELETE}
ON view_name
FOR EACH ROW
BEGIN
EXCEPTION
...
END;

STORED PROCEDURE IN PL/SQL

A stored procedure is a self contained sub programs which are meant for doing some specific well
defined task.

Stored procedure are name PL/SQL blocks which means they can be stored into database as datatbase
object and can be reused.

Unlike PL/SQL Functions, Procedure do not return any value.

There are two types of Sub-Programms

PL/SQL Procedure
PL/SQL Functions

Procedure Syntax:-

CREATE [OR REPLACE] PROCEDURE procedure_name ((Header Section))


(parameter 1,paramter 2....Paramter n)
IS
DECLARE VARIABLE , CONST; ((Execution Section))
BEGIN
<procedure_body>
END;
/

EXAMPLE

create or replace procedure procd1


is
v_name varchar(10):='John';
v_country varchar(16):='UnitedStates';
begin
dbms_output.put_line('Hello I am:'||v_name||' from '||v_country);
end;
/
To run procedure

begin
procd
end;
/

OR

execute procd
OR
exec procd

To view Procedure Body


>select text from user_source where Name=’precedure_name’ order by line;
NOTE:- If make procedure then no declaration required.
NOTE:- Procedure may return a value or may not but function must return value.

=========================================================================

*FUNCTION
The main difference between procedure and a function is, a function must always return a value, and on
the other hand a procedure may or may not return a value. Except this, all the other things of PL/SQL
procedure are true for PL/SQL function too.
*Function Syntax
create or replace function function_name
(paramter 1,paramter 2...Paramter n)
return return_datatype
IS
Declare variable, Const;
Begin
<function_body>
END;
/
*Example of Function
create or replace function square_area(length number) [[header section]]
return number
is
area number(5,3);
begin [[execution sectipn]]
area:=(length * length);
return area;
end;
/

set serveroutput on
TO RUN ABOVE FUNCTION
Begin
dbms_output.put_line('Area of Square='||square_area(5));
end;
/

*IInd Example
create function circle_area(radius number)
return number
is
area number(5,3);
pi constant number(5,3) := 3.14;
begin
area:=(pi * radius * radius);
return area;
end;
/

set serveroutput on
To RUN ABOVE FUNCTION
Begin
dbms_output.put_line('Area of Circle='||circle_area(4));
end;
/

========================================================================
* CURSOR
Oracle creates a memory area, known as the context area.
A cursor is a pointer to this context area.PL/SQL controls the context area through a cursor.A cursor
can hold more than one row,but can process only one row at a time.The set of rows the cursor holds is
called the active set.

There are two types of cursor.


1.Implicit cursors.
2.Explicit cursors.

1) PL/SQL Implicit Cursors


The implicit cursors are automatically generated by Oracle while an SQL statement is executed, if you
don't use an explicit cursor for the statement.
These are created by default to process the statements when DML statements like INSERT, UPDATE,
DELETE etc. are executed.
Oracle provides some attributes known as Implicit cursor's attributes to check the status of DML
operations. Some of them are:
%FOUND
%NOTFOUND
%ROWCOUNT
%ISOPEN.

%FOUND
Its return value is TRUE if DML statements like INSERT, DELETE and UPDATE affect at least one
row or more rows or a SELECT INTO statement returned one or more rows. Otherwise it returns
FALSE.

%NOTFOUND
Its return value is TRUE if DML statements like INSERT, DELETE and UPDATE affect no row, or a
SELECT INTO statement return no rows. Otherwise it returns FALSE. It is a just opposite of
%FOUND.

%ISOPEN
It always returns FALSE for implicit cursors, because the SQL cursor is automatically closed after
executing its associated SQL statements.

%ROWCOUNT
It returns the number of rows affected by DML statements like INSERT, DELETE, and UPDATE or
returned by a SELECT INTO statement.

PL/SQL Implicit Cursor Example

DECLARE
1. total_rows number(2);
2. BEGIN
3. UPDATEcustomers
4. SET salary=salary+5000;
5. IF sql%notfound THEN
6. dbms_output.put_line('no customers updated');
7. ELSIF sql%found THEN
8. total_rows:=sql%rowcount;
9. dbms_output.put_line(total_rows||'customers updated');
10.END IF;
11.END;
12./
2) PL/SQL Explicit Cursors
The Explicit cursors are defined by the programmers to gain more control over the context area. These
cursors should be defined in the declaration section of the PL/SQL block. It is created on a SELECT
statement which returns more than one row.

syntax to create an explicit cursor:


CURSOR cursor_name IS select_statement;

Steps:
You must follow these steps while working with an explicit cursor.
1. Declare the cursor to initialize in the memory.
2. Open the cursor to allocate memory.
3. Fetch the cursor to retrieve data.
4. Close the cursor to release allocated memory.

1) Declare the cursor:


CURSOR name IS SELECT statement;
2) Open the cursor:
OPEN cursor_name;
3) Fetch the cursor:
FETCH cursor_name INTO variable_list;
4) Close the cursor:
Close cursor_name;

PL/SQL Explicit Cursor Example


DECLARE
1. c_id customers.id%type;
2. c_name customers.name%type;
3. c_addr customers.address%type;
4. CURSOR c_customers is
5. SELECT id,name, address FROM customers;
6. BEGIN
7. OPEN c_customers;
8. LOOP
9. FETCH c_customers into c_id, c_name, c_addr;
10.EXIT WHEN c_customers%notfound;
11.dbms_output.put_line(c_id || ' ' || c_name || '' || c_addr);
12.END LOOP;
13.CLOSE c_customers;
14.END;
15./

You might also like