PLSQL

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

PL/SQL is a combination of SQL along with

the procedural features of programming


languages.
It was developed by Oracle Corporation in
the early 90s to enhance the capabilities of
SQL.
The Declaration section (optional).
The Execution section (mandatory).
The Exception Handling (or Error) section
(optional).
The Declaration section of a PL/SQL Block
starts with the reserved keyword DECLARE.
This section is optional and is used to
declare any placeholders like variables,
constants, records and cursors, which are
used to manipulate data in the execution
section.
Placeholders may be any of Variables,
Constants and Records, which stores data
temporarily.
Cursors are also declared in this section.
The Execution section of a PL/SQL Block
starts with the reserved keyword BEGIN and
ends with END.
This is a mandatory section and is the section
where the program logic is written to perform
any task.
The programmatic constructs like loops,
conditional statement and SQL statements
form the part of execution section.
The Exception section of a PL/SQL Block starts with
the reserved keyword EXCEPTION. This section is
optional.
Any errors in the program can be handled in this
section, so that the PL/SQL Blocks terminates
gracefully.
If the PL/SQL Block contains exceptions that cannot
be handled, the Block terminates abruptly with
errors.
Every statement in the above three sections must end
with a semicolon ; .
PL/SQL blocks can be nested within other PL/SQL
blocks. Comments can be used to document code.
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;
The 'Hello World' Example
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
The PL/SQL single-line comments start with the
delimiter -- (double hyphen) and multi-line
comments are enclosed by /* and */.

DECLARE
variable declaration message varchar2(20):= 'Hello,
World!';
BEGIN
/* * PL/SQL executable statement(s) */
dbms_output.put_line(message);
END; /
A cursor is a pointer to this context area.
PL/SQL controls the context area through a
cursor. A cursor holds the rows (one or more)
returned by a SQL statement. The set of rows
the cursor holds is referred to as the active
set.

There are two types of cursors


Implicit cursors
Explicit cursors
Implicit cursors
These are created by default when DML statements
like, INSERT, UPDATE, and DELETE statements are
executed. They are also created when a SELECT
statement that returns just one row is executed.
Explicit cursors
They must be created when you are executing a
SELECT statement that returns more than one row.
Even though the cursor stores multiple records, only
one record can be processed at a time, which is called
as current row. When you fetch a row the current row
position moves to next row.
Both implicit and explicit cursors have the same
functionality, but they differ in the way they are
accessed.
Implicit cursors are automatically created by Oracle whenever an
SQL statement is executed, when there is no explicit cursor for
the statement. Programmers cannot control the implicit cursors
and the information in it.
Whenever a DML statement (INSERT, UPDATE and DELETE) is
issued, an implicit cursor is associated with this statement. For
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.
In PL/SQL, you can refer to the most recent implicit cursor as
the SQL cursor, which always has attributes such as %FOUND,
%ISOPEN, %NOTFOUND, and %ROWCOUNT. The SQL cursor has
additional
attributes, %BULK_ROWCOUNT and %BULK_EXCEPTIONS, designed
for use with the FORALL statement. The following table provides
the description of the most used attributes
S. No Attribute & Description

%FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement
1
affected one or more rows or a SELECT INTO statement
returned one or more rows. Otherwise, it returns FALSE.

%NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an
2 INSERT, UPDATE, or DELETE statement affected no rows, or
a SELECT INTO statement returned no rows. Otherwise, it
returns FALSE.

%ISOPEN
3 Always returns FALSE for implicit cursors, because Oracle
closes the SQL cursor automatically after executing its
associated SQL statement.
%ROWCOUNT
Returns the number of rows affected by an INSERT,
4 UPDATE, or DELETE statement, or returned by a SELECT
INTO statement.
Explicit Cursors
Explicit cursors are programmer-defined cursors for
gaining more control over the context area.

An explicit cursor 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.
CURSOR cursor_name IS select_statement;

Working with an explicit cursor includes the


following steps
Declaring the cursor for initializing the
memory
Opening the cursor for allocating the memory
Fetching the cursor for retrieving the data
Closing the cursor to release the allocated
memory
Declaring the cursor defines the cursor with a name
and the associated SELECT statement. For example

CURSOR c_customers IS SELECT id, name,


address FROM customers;
1> DECLARE
2> emp_rec emp_tbl%rowtype;
3> CURSOR emp_cur IS
4> SELECT *
5> FROM
6> WHERE salary > 10;
7> BEGIN
8> OPEN emp_cur;
9> FETCH emp_cur INTO emp_rec;
10> dbms_output.put_line (emp_rec.first_name || ' ' ||
emp_rec.last_name);
11> CLOSE emp_cur;
12> END;
first we are creating a record emp_rec of the
same structure as of table emp_tbl in line no 2.
We can also create a record with a cursor by
replacing the table name with the cursor name.
Second, we are declaring a cursor emp_cur from
a select query in line no 3 - 6.
Third, we are opening the cursor in the execution
section in line no 8.
Fourth, we are fetching the cursor to the record
in line no 9.
Fifth, we are displaying the first_name and
last_name of the employee in the record emp_rec
in line no 10.
Sixth, we are closing the cursor in line no 11.
Exceptions (PL/SQL runtime errors) can arise
from design faults, coding mistakes,
hardware failures, and many other sources.
You cannot anticipate all possible exceptions,
but you can write exception handlers that let
your program to continue to operate in their
presence.
Any PL/SQL block can have an exception-
handling part, which can have one or more
exception handlers. For example, an exception-
handling part could have this syntax:
EXCEPTION
WHEN ex_name_1 THEN statements_1 -- Exception
handler
WHEN ex_name_2 OR ex_name_3
THEN statements_2 -- Exception handler
WHEN OTHERS THEN statements_3 -- Exception handler
END;
In the preceding syntax example, ex_name_n is
the name of an exception and statements_n is
one or more statements.

You might also like