Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
By,
Asmatullah Khan,
CL/CP, GIOE,
Secunderabad.
Outline
PL/SQL
• PL/SQL stands for Procedural Language extensions
to the Structured Query Language (SQL).
• All the PL/SQL units are treated as PL/SQL blocks, and this is the starting
stage of the architecture which serves as the primary input.
• It separates PL/SQL
units and SQL part in the
input.
• It consists of SQL
executor which actually
parses the input SQL
statements and execute
the same.
PL/SQL Features
• PL/SQL is tightly integrated with SQL.
• PL/SQL allows sending an entire block of statements to the database at one time. This reduces
network traffic and provides high performance for the applications.
• PL/SQL gives high productivity to programmers as it can query, transform, and update data in a
database.
• PL/SQL saves time on design and debugging by strong features, such as exception handling,
encapsulation, data hiding, and object-oriented data types.
SQL PL/SQL
PL/SQL is a block of codes that used to
SQL is a single query that is used to
write the entire program blocks/
perform DML and DDL operations.
procedure/ function, etc.
• Only the execution section is mandatory and all others are optional.
▫ The declaration section allows you to define data types, structures, and
variables.
You often declare variables in the declaration section by giving them names, data
types, and initial values.
▫ The execution section is required in a block structure and it must have at least
one statement.
The execution section is the place where you put the execution code or business
logic code.
You can use both procedural and SQL statements inside the execution section.
BEGIN
DBMS_OUTPUT.put_line (UPPER (l_company_name));
DBMS_OUTPUT.put_line (LOWER (l_company_name));
DBMS_OUTPUT.put_line (INITCAP (l_company_name));
END;
/
OUTPUT:
ORACLE CORPORATION
oracle corporation
Oracle Corporation
DECLARE
l_company_name VARCHAR2 (6) := 'Oracle';
BEGIN
/* Retrieve the first character in the string */
DBMS_OUTPUT.put_line (SUBSTR (l_company_name, 1, 1));
/* Retrieve the last character in the string */
DBMS_OUTPUT.put_line (SUBSTR (l_company_name, -1, 1));
/* Retrieve three characters, starting from the second position. */
DBMS_OUTPUT.put_line (SUBSTR (l_company_name, 2, 3));
/* Retrieve the remainder of the string, starting from the second position. */
DBMS_OUTPUT.put_line (SUBSTR (l_company_name, 2));
END;
/
DBMS_OUTPUT.put_line ('Header');
DBMS_OUTPUT.put_line (LPAD ('Sub-header', 13, '.'));
/* Add "123" to the end of the string, until the 20 character is reached.*/
DBMS_OUTPUT.put_line (RPAD ('abc', 20, '123'));
• PL/SQL tables can consist of one simple •Rows in a PL/SQL table do not have to be
datatype or be defined as a type of record and is contiguous.
sometimes referred to as an Index by table.
PL/SQL Decision Making Statements
• Decision-making structures require that the programmer specify one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and optionally,
other statements to be executed if the condition is determined to be false.
Statement Description
The IF statement associates a condition with a sequence of statements enclosed by the
IF - THEN
keywords THEN and END IF. If the condition is true, the statements get executed and if
statement
the condition is false or NULL then the IF statement does nothing.
IF-THEN-ELSIF
It allows you to choose between several alternatives.
statement
Like the IF statement, the CASE statement selects one sequence of statements to execute.
However, to select the sequence, the CASE statement uses a selector rather than multiple
Case statement
Boolean expressions. A selector is an expression whose value is used to select one of several
alternatives.
Searched CASE The searched CASE statement has no selector, and it's WHEN clauses contain search
statement conditions that yield Boolean values.
nested IF-THEN- You can use one IF-THEN or IF-THEN-ELSIF statement inside another IF-THEN or IF-
ELSE THEN-ELSIF statement(s).
PL/SQL IF - THEN statement Example 1
PL/SQL IF - THEN statement Example 2
PL/SQL IF-THEN-ELSE statement Example
PL/SQL IF-THEN-ELSIF statement Example
PL/SQL Case statement Example
PL/SQL Searched Case statement Example
PL/SQL nested IF-THEN-ELSE statement Example
PL/SQL Loops
Loop Control
Type
Description Description
Statement
The Exit statement
In this loop structure, sequence of statements completes the loop
PL/SQL is enclosed between the LOOP and END EXIT and control passes to
Basic LOOP statements. At each iteration, the statement the statement
LOOP sequence of statements is executed and then
immediately after
control resumes at the top of the loop.
END LOOP
Causes the loop to
skip the remainder of
PL/SQL Repeats a statement or group of statements
WHILE while a given condition is true. It tests the CONTINUE its body and
LOOP condition before executing the loop body. statement immediately retest its
condition prior to
reiterating.
Execute a sequence of statements multiple
PL/SQL Transfers control to
times and abbreviates the code that manages
FOR LOOP the labeled statement.
the loop variable.
GOTO Though it is not
Nested statement advised to use GOTO
You can use one or more loop inside any
loops in statement in your
another basic loop, while or for loop.
PL/SQL program.
Labeling loops
PL/SQL Basic LOOP Example 1
PL/SQL Basic LOOP Example 2
PL/SQL WHILE Loop Example
PL/SQL FOR Loop Example
PL/SQL NESTED Loops Example
PL/SQL Continue Example
PL/SQL GOTO Example
An error condition during a program execution is called an exception in PL/SQL.
PL/SQL supports programmers to catch such conditions using EXCEPTION block in the program and an appropriate action
is taken against the error condition.
It is raised when a program attempts to apply collection methods other than EXISTS to an
COLLECTION_IS_NULL uninitialized nested table or varray, or the program attempts to assign values to the elements of
an uninitialized nested table or varray.
DUP_VAL_ON_INDEX It is raised when duplicate values are attempted to be stored in a column with unique index.
It is raised when attempts are made to make a cursor operation that is not allowed, such as
INVALID_CURSOR
closing an unopened cursor.
It is raised when the conversion of a character string into a number fails because the string does
INVALID_NUMBER
not represent a valid number.
It is raised when s program attempts to log on to the database with an invalid username or
LOGIN_DENIED
password.
NO_DATA_FOUND It is raised when a SELECT INTO statement returns no rows.
NOT_LOGGED_ON It is raised when a database call is issued without being connected to the database.
PROGRAM_ERROR It is raised when PL/SQL has an internal problem.
ROWTYPE_MISMATCH It is raised when a cursor fetches value in a variable having incompatible data type.
It is raised when a member method is invoked, but the instance of the object type was not
SELF_IS_NULL
initialized.
STORAGE_ERROR It is raised when PL/SQL ran out of memory or memory was corrupted.
TOO_MANY_ROWS It is raised when s SELECT INTO statement returns more than one row.
• The optional parameter list contains name, mode and types of the parameters.
▫ IN represents that value will be passed from outside and
▫ OUT represents that this parameter will be used to return a value outside of the
procedure.
Note: The AS keyword is used instead of the IS keyword for creating a standalone
procedure.
PL/SQL Procedure Example
Parameter Modes
Mode Parameter Mode & Description