PLSQL Tutorialspoint
PLSQL Tutorialspoint
PLSQL Tutorialspoint
{IS | AS}
BEGIN
END procedure_name;
Where,
The optional parameter list contains name, mode and types of the
parameters. IN represents the value that will be passed from outside and OUT
represents the parameter that will be used to return a value outside of the
procedure.
Example
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
produce the following result shown by –
BEGIN
greetings;
END;
Deleting Procedure
DECLARE
a number;
b number;
c number;
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
END;
a number;
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
END;
Function
Syntax for the CREATE FUNCTION statement is as follows:-
Where,
The optional parameter list contains name, mode and types of the parameters.
IN represents the value that will be passed from outside and OUT represents the
parameter that will be used to return a value outside of the procedure.
The RETURN clause specifies the data type you are going to return from the
function.
total number(2) := 0;
BEGIN
FROM customers;
RETURN total;
END;
DECLARE
c number(2);
BEGIN
c := totalCustomers();
END;
Example
DECLARE
a number;
b number;
c number;
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
END;
Cursors
Oracle creates a memory area, known as the context area.
Implicit cursors
Explicit cursors
Implicit Cursors
Implicit cursors are automatically created by Oracle.
There is no explicit cursor for the statement. Programmers cannot
control the implicit cursors and the information in it.
INSERT operations, the cursor holds the data that needs to be inserted.
For UPDATE and DELETE operations, the cursor identifies the rows
that would be affected
%FOUND
%NOTFOUND
3 Always returns FALSE for implicit cursors, because Oracle closes the SQL
cursor automatically after executing its associated SQL statement.
%ROWCOUNT
Example:
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
IF sql%notfound THEN
total_rows := sql%rowcount;
END IF;
END;
Explicit Cursors
CURSOR c_customers IS
OPEN c_customers;
CLOSE c_customers;
Example:
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
BEGIN
OPEN c_customers;
LOOP
END LOOP;
CLOSE c_customers;
END;
DECLARE
c_id customers.id%type;
c_name customerS.name%type;
c_addr customers.address%type;
CURSOR c_customers is
BEGIN
OPEN c_customers;
LOOP
END LOOP;
CLOSE c_customers;
END;
Records
A record is a data structure that can hold data items of different
kinds.
Records consist of different fields, similar to a row of a database
table.
Table-based
Cursor-based records
User-defined records
Table-based
%ROWTYPE attribute enables a programmer to create table-
based and cursor based records.
Example:
DECLARE
customer_rec customers%rowtype;
BEGIN
FROM customers
WHERE id = 5;
END;
Cursor-based records:
Using the cursor , retrieved the record and example :
DECLARE
CURSOR customer_cur is
FROM customers;
customer_rec customer_cur%rowtype;
BEGIN
OPEN customer_cur;
LOOP
END LOOP;
END;
User-Defined Records:
User-defined record type that allows you to define the different
record structures.
These records consist of different fields,
Suppose you want to keep track of your books in a library.
following attributes about each book −
Title
Author
Subject
Book ID
DECLARE
(title varchar(50),
author varchar(50),
subject varchar(100),
book_id number);
book1 books;
book2 books;
Example: Accessing this field
DECLARE
(title varchar(50),
author varchar(50),
subject varchar(100),
book_id number);
book1 books;
book2 books;
BEGIN
-- Book 1 specification
book1.book_id := 6495407;
-- Book 2 specification
book2.book_id := 6495700;
END;
Exceptions
System-defined exceptions
User-defined exceptions
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
exception1-handling-statements
exception2-handling-statements
exception3-handling-statements
........
exception3-handling-statements
END;
Example:
DECLARE
c_id customers.id%type := 8;
c_name customerS.Name%type;
c_addr customers.address%type;
BEGIN
FROM customers
WHERE id = c_id;
EXCEPTION
dbms_output.put_line('Error!');
END;
Raising Exceptions:
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
statement;
END;
Example :
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
-- user defined exception
ex_invalid_id EXCEPTION;
BEGIN
RAISE ex_invalid_id;
ELSE
FROM customers
WHERE id = c_id;
END IF;
EXCEPTION
dbms_output.put_line('Error!');
END;
DECLARE
c_name customerS.Name%type;
c_addr customers.address%type;
ex_invalid_id EXCEPTION;
BEGIN
IF c_id <= 0 THEN
RAISE ex_invalid_id;
ELSE
FROM customers
WHERE id = c_id;
END IF;
EXCEPTION
dbms_output.put_line('Error!');
END;
Triggers
Benefits of Triggers
Triggers can be written for the following purposes −
[OF col_name]
ON table_name
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
Where,
{BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will be
executed. The INSTEAD OF clause is used for creating trigger on a view.
{INSERT [OR] | UPDATE [OR] | DELETE} − This specifies the DML operation.
[OF col_name] − This specifies the column name that will be updated.
[ON table_name] − This specifies the name of the table associated with the
trigger.
[REFERENCING OLD AS o NEW AS n] − This allows you to refer new and old values
for various DML statements, such as INSERT, UPDATE, and DELETE.
[FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will be
executed for each row being affected. Otherwise the trigger will execute just once
when the SQL statement is executed, which is called a table level trigger.
WHEN (condition) − This provides a condition for rows for which the trigger would
fire. This clause is valid only for row-level triggers.