PL-SQL in DBMS
PL-SQL in DBMS
1
Department of Computer Applications
PL/SQL Introduction
• PL/SQL is a block structured language that enables developers to combine the power
of SQL with procedural statements.
• All the statements of a block are passed to oracle engine all at once which increases
processing speed and decreases the traffic.
2
Department of Computer Applications
PL/SQL Introduction
• PL/SQL stands for Procedural Language extensions to the Structured Query
Language (SQL).
• PL/SQL includes procedural language elements like conditions and loops. It allows
declaration of constants and variables, procedures and functions, types and variable
of those types and triggers.
Features of PL/SQL
• PL/SQL is basically a procedural language, which provides the functionality of
decision making, iteration and many more features of procedural programming
languages.
• PL/SQL can execute a number of queries in one block using single command.
• One can create a PL/SQL unit such as procedures, functions, packages, triggers, and
types, which are stored in the database for reuse by applications.
• PL/SQL provides a feature to handle the exception which occurs in PL/SQL block
known as exception handling block.
• Applications written in PL/SQL are portable to computer hardware or operating
system where Oracle is operational.
• PL/SQL Offers extensive error checking.
Disadvantages of SQL
• SQL doesn’t provide the programmers with a technique of condition checking,
looping and branching.
• SQL statements are passed to Oracle engine one at a time which increases traffic
and decreases speed.
• PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural
language that is more powerful than SQL. The basic unit in PL/SQL is a block. All PL/SQL programs are
made up of blocks, which can be nested within each other.
Structure of PL/SQL Block
• Typically, each block performs a logical action in the program. A block has the
following structure:
Structure of PL/SQL Block
• Declare section starts with DECLARE keyword in which variables, constants, records
as cursors can be declared which stores data temporarily. It basically consists
definition of PL/SQL identifiers. This part of the code is optional.
• Execution section starts with BEGIN and ends with END keyword.This is a mandatory
section and here the program logic is written to perform any task like loops and
conditional statements. It supports all DML commands, DDL commands and
SQL*PLUS built-in functions as well.
• It is nothing except a name given to a storage area. Each variable in the PL/SQL has a
specific data type which defines the size and layout of the variable's memory.
• A variable should not exceed 30 characters. Its letter optionally followed by more
letters, dollar signs, numerals, underscore etc.
How to declare variable in PL/SQL
• You must declare the PL/SQL variable in the declaration section or in a package as a
global variable. After the declaration, PL/SQL allocates memory for the variable's
value and the storage location is identified by the variable name.
• Syntax for declaring variable:
Naming rules for PL/SQL variables
• The variable in PL/SQL must follow some naming rules like other programming languages.
• Variable name should not be the same as the table table's column of that block.
• The name of the variable must begin with ASCII letter. The PL/SQL is not case sensitive so it
could be either lowercase or uppercase. For example: v_data and V_DATA refer to the same
variables.
• You should make your variable easy to read and understand, after the first character, it may
be any number, underscore (_) or dollar sign ($).
• It contains all information needed for processing the statement. In PL/SQL, the
context area is controlled by Cursor.
• A cursor contains information on a select statement and the rows of data accessed by
it.
Cursor
A cursor is used to referred to a program to fetch and process the rows returned by the
SQL statement, one at a time. There are two types of cursors:
1. Implicit Cursors
2. Explicit Cursors
Implicit Cursors
• The implicit cursors are automatically generated by Oracle while an SQL statement is
executed, if you don't use an explicit cursor for the statement.
• These are created by default to process the statements when DML statements like
INSERT, UPDATE, DELETE etc. are executed.
• Orcale provides some attributes known as Implicit cursor's attributes to check the
status of DML operations. Some of them are: %FOUND, %NOTFOUND, %ROWCOUNT
and %ISOPEN.
Implicit Cursors
• For example: When you execute the SQL statements like INSERT, UPDATE, DELETE then the cursor attributes tell
whether any rows are affected and how many have been affected. If you run a SELECT INTO statement in PL/SQL
block, the implicit cursor attribute can be used to find out whether any row has been returned by the SELECT
statement. It will return an error if there no data is selected.
THANK YOU
18
Department of Computer Applications