PL SQL N
PL SQL N
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).
EXAMPLE:-
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.
PL/SQL Procedure
PL/SQL Functions
Procedure Syntax:-
EXAMPLE
begin
procd
end;
/
OR
execute procd
OR
exec procd
=========================================================================
*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.
%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.
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.
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.