0% found this document useful (0 votes)
44 views115 pages

Advance Database Management System: Unit - 2 - Procedural Language/Structured Query Language (PL/SQL)

This document provides an overview of PL/SQL (Procedural Language/Structured Query Language). It discusses PL/SQL blocks, data types, variables, control structures like loops and conditions, procedures, functions and other PL/SQL concepts. PL/SQL extends SQL by adding programming constructs like conditions and loops. It allows developers to group SQL statements and PL/SQL code into reusable blocks that can be stored in an Oracle database.

Uploaded by

KARTIKAY LADDHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views115 pages

Advance Database Management System: Unit - 2 - Procedural Language/Structured Query Language (PL/SQL)

This document provides an overview of PL/SQL (Procedural Language/Structured Query Language). It discusses PL/SQL blocks, data types, variables, control structures like loops and conditions, procedures, functions and other PL/SQL concepts. PL/SQL extends SQL by adding programming constructs like conditions and loops. It allows developers to group SQL statements and PL/SQL code into reusable blocks that can be stored in an Oracle database.

Uploaded by

KARTIKAY LADDHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 115

Advance Database

Management System
Unit – 2 . Procedural Language/Structured Query language (PL/SQL)
.
Index

▪ Introduction to PL/SQL
▪ Disadvantages of SQL and advantages of PL/SQL
▪ PL/SQL block structure
▪ Block data types, Block variable declaration and exception handling
▪ Cursors and types of cursors
▪ Functions, Procedures and Triggers.
Introduction to PL/SQL

▪ The PL/SQL procedural extension language for SQL and the Oracle relational
database
▪ Has features of programming language.
▪ SQL command are included in procedure block
▪ PL/SQl code are executed by procedure statement executer and SQL command s
are executed by SQL statement executor
PL/SQL Block

▪ PL/SQL blocks have a pre-defined structure in which the code is to be


grouped. Below are different sections of PL/SQL blocks.
1. Declaration section
2. Execution section
3. Exception-Handling section
PL/SQL Block structure
The 'Hello World' Example

set serveroutput on /* to display output on screen *?


dbms_output.put_line(message);
END;
/
Note: place a semicolon at end of each statement, (/) it is used to run
anonymous PL/SQL block.
END statement require semicolon.
The 'Hello World' Example

DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
Advantages of PL/SQL

▪ SQL is the standard database language and PL/SQL is strongly integrated with
SQL. PL/SQL supports both static and dynamic SQL. Static SQL supports DML
operations and transaction control from PL/SQL block.
▪ In Dynamic SQL, SQL allows embedding DDL statements in PL/SQL
blocks.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.
Advantages of PL/SQL

▪ Applications written in PL/SQL are fully portable.


▪ PL/SQL provides high security level.
▪ PL/SQL provides access to predefined SQL packages.
▪ PL/SQL provides support for Object-Oriented Programming.
▪ PL/SQL provides support for developing Web Applications and Server
Pages.
Types of PL/SQL block

PL/SQL blocks are of mainly two types.


Anonymous blocks
Anonymous blocks are PL/SQL blocks which do not have any names
assigned to them. They need to be created and used in the same
session because they will not be stored in the server as database
objects.
Since they need not store in the database, they need no compilation
steps. They are written and executed directly, and compilation and
execution happen in a single process.
Named blocks:
▪ Named blocks have a specific and unique name for them. They are
stored as the database objects in the server. Since they are available
as database objects, they can be referred to or used as long as it is
present on the server. The compilation process for named blocks
happens separately while creating them as a database objects.
Named blocks are basically of two types:
▪ Procedure
▪ Function
PL/SQL data types

▪ PL/SQL has two kinds of data types: scalar and composite.


▪ The scalar types are types that store single values such as number,
Boolean, character, and date time
▪ whereas the composite types are types that store multiple values,
for example, record and collection.ge of values.
PL/SQL Data types
PL/SQL Boolean Data Types

▪ The BOOLEAN data type stores logical values that are used in logical
operations. The logical values are the Boolean values TRUE and
FALSE and the value NULL.
▪ However, SQL has no data type equivalent to BOOLEAN. Therefore,
Boolean values cannot be used in −
▪ SQL statements
▪ Built-in SQL functions (such as TO_CHAR)
▪ PL/SQL functions invoked from SQL statements
PL/SQL Large Object (LOB) Data Types

▪ Large Object (LOB) data types refer to large data items such as text,
graphic images, video clips, and sound waveforms. LOB data types
allow efficient, random, piecewise access to this data. Following are
the predefined PL/SQL LOB data types −
PL/SQL Datetime and Interval Types
PL/SQL User-Defined Subtypes

▪ PL/SQL predefines several subtypes in package STANDARD. For


example, PL/SQL predefines the subtypes CHARACTER and
INTEGER as follows −

▪ SUBTYPE CHARACTER IS CHAR;


▪ SUBTYPE INTEGER IS NUMBER(38,0);
DECLARE
SUBTYPE name IS char(20);
SUBTYPE message IS varchar2(100);
salutation name;
greetings message;
BEGIN
salutation := 'Reader ';
greetings := 'Welcome to the World of PL/SQL';
dbms_output.put_line('Hello ' || salutation || greetings);
END;
/
NULLs in PL/SQL

▪ PL/SQL NULL values represent missing or unknown data and they


are not an integer, a character, or any other specific data type. Note
that NULL is not the same as an empty data string or the null
character value '\0'. A null can be assigned but it cannot be equated
with anything, including itself.
PL-SQL

▪ declare
var1 varchar2(20);
num1 number(10);
begin
var1:=‘welcome’;
num1:=1000;
dbms_output.put_line(‘Var := ' || var1);
dbms_output.put_line(‘num1 := ' || num1);
end;
/
PL/SQL statement to take value from
table and put in variable and display
it

▪ declare
name varchar2(20);
salary number(10,2);
begin
select FirstName,Salary into name, salary from employee where Employeeid=10;
dbms_output.put_line(‘Name := ' || name);
dbms_output.put_line(‘Salary := ' || salary);
end;
/
TYPE
ROWTYPE
Variable Declaration

▪ PL/SQL variables must be declared in the declaration section or in a


package as a global variable. When you declare a variable, PL/SQL
allocates memory for the variable's value and the storage location is
identified by the variable name.
▪ When you provide a size, scale or precision limit with the data type, it
is called a constrained declaration. Constrained declarations require
less memory than unconstrained declarations.
Initializing Variables in PL/SQL

▪ Whenever you declare a variable, PL/SQL assigns it a default value of


NULL. If you want to initialize a variable with a value other than the
NULL value, you can do so during the declaration, using either of the
following −
▪ The DEFAULT keyword
▪ The assignment operator
For example
counter binary_integer := 0;
greetings varchar2(20) DEFAULT 'Have a Good Day';
Variable Scope in PL/SQL

▪ There are two types of variable scope −


▪ Local variables − Variables declared in an inner block and not
accessible to outer blocks.
▪ Global variables − Variables declared in the outermost block or a
package.
Declaring a Constant
Operator

▪ An operator is a symbol that tells the compiler to perform specific


mathematical or logical manipulation. PL/SQL language is rich in
built-in operators and provides the following types of operators −
▪ Arithmetic operators
▪ Relational operators
▪ Comparison operators
▪ Logical operators
▪ String operators
Arithmetic Operators
PL/SQL Operator Precedence
Relational operator
Comparison Operator
Logical operator
Condition

▪ 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.
▪ IF-THEN
▪  IF-THEN-ELSE
▪ IF-THEN-ELSIF
▪ CASE statement
▪ searched CASE statement
▪ nested IF-THEN-ELSE
IF-THEN

▪ The IF statement associates a condition with a sequence of


statements enclosed by the keywords THEN and END IF.
▪ If the condition is TRUE, the statements get executed, and if the
condition is FALSE or NULL, then the IF statement does nothing.
 IF-THEN-ELSE 
IF-THEN-ELSIF 
the CASE statement

▪ the CASE statement uses a selector rather than multiple Boolean


expressions. A selector is an expression, the value of which is used to
select one of several alternatives.
searched CASE statement
nested IF-THEN-ELSE
nested IF-THEN-ELSE
PL/SQL Loops

▪ A loop statement allows us to execute a statement or group of


statements multiple times .
Basic Loop statement

▪ Basic loop structure encloses sequence of statements in between the


LOOP and END LOOP statements. With each iteration, the sequence
of statements is executed and then control resumes at the top of the
loop.
exit WHEN
WHILE LOOP
PL/SQL - FOR LOOP Statement
PL/SQL - Nested Loops
Labeling a PL/SQL Loop
The Loop Control Statements
EXIT statement
EXIT statement
EXIT-WHEN
EXIT-WHEN
Continue statement
Continue statement
PL/SQL - GOTO Statement
PL/SQL - Strings
PL/SQL String Functions and Operators
PL/SQL - Arrays
Creating a Varray Type
PL/SQL - Procedures

▪ The PL/SQL stored procedure or simply a procedure is a PL/SQL


block which performs one or more specific tasks. It is just like
procedures in other programming languages.
The procedure contains a header and a body.
▪ Header: The header contains the name of the procedure and the
parameters or variables passed to the procedure.
▪ Body: The body contains a declaration section, execution section and
exception section similar to a general PL/SQL block.
How to pass parameters in procedure

There is three ways to pass parameters in procedure:


▪ IN parameters: The IN parameter can be referenced by the procedure
or function. The value of the parameter cannot be overwritten by the
procedure or the function.
▪ OUT parameters: The OUT parameter cannot be referenced by the
procedure or function, but the value of the parameter can be
overwritten by the procedure or function.
▪ INOUT parameters: The INOUT parameter can be referenced by the
procedure or function and the value of the parameter can be
overwritten by the procedure or function.
PL/SQL Create Procedure
PL/SQL Function
Another PL/SQL Function Example
Calling PL/SQL Function
PL/SQL Recursive Function

You might also like