Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Introduction to PL/SQL

(Procedure and Cursor)

SOURCE:
https://docs.oracle.com/cloud/latest/db112/LNPLS/triggers.htm#LNPLS723
PL/SQL

▪ PL/SQL is Oracle's procedural language extension to SQL.


▪ PL/SQL combines the data manipulating power of SQL with the
data processing power of procedural languages.
▪ Like other procedural programming languages, PL/SQL lets you
declare constants and variables, control program flow, define
subprograms, and trap runtime errors.
▪ You can break complex problems into easily understandable
subprograms, which you can reuse in multiple applications.
ADVANTAGES OF PL/SQL
➢Tight Integration with SQL
 PL/SQL lets us use all SQL data manipulation, and all SQL
functions, operators, and pseudocolumns.
 PL/SQL fully supports SQL data types.
 For example, if your PL/SQL program retrieves a value from
a column of the SQL type VARCHAR2, it can store that value
in a PL/SQL variable of the type VARCHAR2.
➢ High Performance
 PL/SQL lets you send a block of statements to the database,
significantly reducing traffic between the application and the
database.
 PL/SQL subprograms are stored in executable form, which can
be invoked repeatedly.
 Because stored subprograms run in the database server, a single
invocation over the network can start a large job.
ADVANTAGES OF PL/SQL
➢Portability
 We can run PL/SQL applications on any operating system and
platform where Oracle Database runs.
➢Support for Object-Oriented Programming
 PL/SQL supports object-oriented programming
DIFFERENCE BETWEEN PL/SQL AND SQL
▪A single SQL statement issued on the client computer, causes
two trips on the network.
▪Multiple SELECT statements issued on the network increase
the traffic significantly very fast.
▪For example, four SELECT statements cause eight network trips

▪Statements that are part of the PL/SQL block, are sent to the
server as a single unit.
▪The SQL statements in this PL/SQL program are executed at the server
and the result set is sent back as a single unit.
PL/SQL BLOCKS
• PL/SQL blocks can be divided into two groups:
 Named and
 Anonymous.
▪ Named blocks are used when creating subroutines. These
subroutines are procedures, functions, and packages.
▪ The subroutines can be stored in the database and referenced by
their names later on.
▪ In addition, subroutines can be defined within the anonymous
PL/SQL block.
▪ Anonymous PL/SQL blocks do not have names. As a result,
they cannot be stored in the database and referenced later.
PL/SQL BLOCK STRUCTURE

PL/SQL blocks contain three sections


 Declare section
 Executable section and
 Exception-handling section
▪The executable section is the only mandatory section of
the block.
▪Both the declaration and exception-handling sections are
optional
Anonymous PL/SQL block has the following structure:
DECLARE
Declaration statements
BEGIN
Executable statements
EXCEPTION
Exception-handling statements
END ;
DECLARATION SECTION

▪The declaration section is the first section of the


PL/SQL block.
▪It contains definitions of PL/SQL identifiers such as
variables, constants, cursors and so on
Example
DECLARE
v_first_name VARCHAR2(35) ;
v_last_name VARCHAR2(35) ;
v_counter NUMBER := 0 ;
EXECUTABLE SECTION
▪The executable section is the next section of the PL/SQL block
after declare section.
▪This section contains executable statements that allow you to
manipulate the variables that have been declared in the
declaration section
BEGIN
SELECT first_name, last_name
INTO v_first_name, v_last_name
FROM student
WHERE student_id = 123 ;
DBMS_OUTPUT.PUT_LINE
(‘Student name :’ || v_first_name ||‘ ’|| v_last_name);
END;
EXCEPTION-HANDLING SECTION
▪The exception-handling section is the last section of the PL/SQL
block.
▪An exception is a PL/SQL error that is raised during program
execution, either implicitly or explicitly by your program.
▪Handle an exception by trapping it with a handler or propagating it to
the calling environment.
▪There are two categories of exceptions in the world of PL/SQL:
predefined and user-defined.
➢A predefined exception is an internally defined exception that is
assigned a name by PL/SQL.
➢A user-defined exception is one you have declared in the
declaration section of a program unit. User-defined exceptions can
be associated with an internally defined exception (that is, you can
give a name to an otherwise unnamed exception) or with an
application-specific error.

You might also like