0% found this document useful (0 votes)
0 views

PL-SQL in DBMS

PL/SQL is a block-structured language that combines SQL with procedural programming features, enhancing processing speed and reducing traffic by executing statements in blocks. It includes elements like conditions, loops, and exception handling, allowing for the creation of reusable database units such as procedures and functions. The document also discusses the structure of PL/SQL blocks, variable declaration, and the use of cursors for processing SQL statements.

Uploaded by

gauravmandal165
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

PL-SQL in DBMS

PL/SQL is a block-structured language that combines SQL with procedural programming features, enhancing processing speed and reducing traffic by executing statements in blocks. It includes elements like conditions, loops, and exception handling, allowing for the creation of reusable database units such as procedures and functions. The document also discusses the structure of PL/SQL blocks, variable declaration, and the use of cursors for processing SQL statements.

Uploaded by

gauravmandal165
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Department of Computer Applications.

Presented by: Ruchi Sharma


Assistant Professor
Computer Applications

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 is a combination of SQL along with the procedural features of programming


languages.

• Oracle uses a PL/SQL engine to processes the PL/SQL statements.

• 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.

• SQL has no facility of error checking during manipulation of data.


Differences between SQL and PL/SQL
Structure of PL/SQL Block

• 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.

• Exception section starts with EXCEPTION keyword.This section is optional which


contains statements that are executed when a run-time error occurs. Any exceptions
can be handled in this section.
PL/SQL Variables

• A variable is a meaningful name which facilitates a programmer to store data


temporarily during the execution of code. It helps you to manipulate data in PL/SQL
programs.

• 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.

• The variable_name should not exceed 30 characters.

• 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 ($).

• NOT NULL is an optional specification on the variable.


Initializing Variables in PL/SQL
• Evertime you declare a variable, PL/SQL defines a default value NULL to it. If you want
to initialize a variable with other value than NULL value, you can do so during the
declaration, by using any one of the following methods.

• The DEFAULT keyword

• The assignment operator


Cursor
• When an SQL statement is processed, Oracle creates a memory area known as
context area.
• A cursor is a pointer to this context area.

• 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

You might also like