0% found this document useful (0 votes)
28 views8 pages

PL/SQL Concepts: PL/SQL Introduction Block-Structured Language Oracle SQL Oracle Database PL/SQL Basics SQL

PL/SQL is a procedural extension of SQL developed by Oracle that allows for efficient data manipulation and control-flow logic within the Oracle Database. It features a block structure that includes declaration, execution, and exception handling sections, and supports the creation of reusable units like procedures and functions. Key components include cursors for row-by-row data retrieval and stored procedures for compiled code execution, with advantages such as error handling and flexibility, but limitations like vendor dependency.

Uploaded by

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

PL/SQL Concepts: PL/SQL Introduction Block-Structured Language Oracle SQL Oracle Database PL/SQL Basics SQL

PL/SQL is a procedural extension of SQL developed by Oracle that allows for efficient data manipulation and control-flow logic within the Oracle Database. It features a block structure that includes declaration, execution, and exception handling sections, and supports the creation of reusable units like procedures and functions. Key components include cursors for row-by-row data retrieval and stored procedures for compiled code execution, with advantages such as error handling and flexibility, but limitations like vendor dependency.

Uploaded by

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

PL/SQL Concepts

PL/SQL Introduction
PL/SQL (Procedural Language/Structured Query Language) is a block-structured
language developed by Oracle that allows developers to combine the power
of SQL with procedural programming constructs. The PL/SQL language enables
efficient data manipulation and control-flow logic, all within the Oracle Database.
In this article, we’ll cover PL/SQL basics, including its core features, PL/SQL block
structure, and practical examples that demonstrate the power of PL/SQL. We’ll also
explore the differences between SQL and PL/SQL, how variables and identifiers
work, and how the PL/SQL execution environment operates within Oracle.
Basics of PL/SQL
 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 process 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
1. PL/SQL is basically a procedural language, which provides the functionality
of decision-making, iteration, and many more features of procedural
programming languages.
2. PL/SQL can execute a number of queries in one block using single command.
3. 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.
4. PL/SQL provides a feature to handle the exception which occurs in PL/SQL
block known as exception handling block.
5. Applications written in PL/SQL are portable to computer hardware or
operating system where Oracle is operational.
6. PL/SQL Offers extensive error checking.

Differences Between SQL and PL/SQL


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.

It is declarative, that defines what


PL/SQL is procedural that defines how
needs to be done, rather than how
the things needs to be done.
things need to be done.

Execute as a single statement. Execute as a whole block.

Mainly used to manipulate data. Mainly used to create an application.

It is an extension of SQL, so it can


Cannot contain PL/SQL code in it.
contain SQL inside it.

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.
Typically, each block performs a logical action in the program. A block has the
following structure:

DECLARE
declaration statements;

BEGIN
executable statements

EXCEPTIONS
exception handling statements

END;
 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.
Applications:
1. Batch processing for large datasets.
2. Business rule implementation.
3. Scheduled database maintenance tasks.

Advantages
1)PL/SQL offers procedural methods of code writing for accessing the
database and manipulating the data with flexibility.
2)PL/SQL is development tool that are not only support SQL data
manipulation but also provides facilities of conditional checking,
branching and looping.
3)PL/SQL also permits dealing with errors as required and display user
friendly messages when errors are occurred.
4)PL/SQL allows to declare variables and use them in the block of code.
5)Programmers can write interactive programs that accept input from the
user.

Disadvantages:-
1)PL/SQL is proprietary i.e. personal to oracle which means if you change
database vendors then you have to rewrite all your PL/SQL programs.
2)The other limitation of oracle is there is very little support of input to
read and write files.
Cursor
A Cursor in PL/SQL is a pointer to a context area that stores the result set
of a query.
PL/SQL Cursors
The cursor is used to retrieve data one row at a time from the results set,
unlike other SQL commands that operate on all rows at once.
Cursors update table records in a singleton or row-by-row manner.
The Data that is stored in the Cursor is called the Active Data Set. Oracle
DBMS has another predefined area in the main memory Set, within which
the cursors are opened. Hence the size of the cursor is limited by the size
of this pre-defined area.

Cursor Actions
Key actions involved in working with cursors in PL/SQL are:
1. Declare Cursor: A cursor is declared by defining the SQL statement
that returns a result set.
2. Open: A Cursor is opened and populated by executing the SQL
statement defined by the cursor.
3. Fetch: When the cursor is opened, rows can be fetched from the
cursor one by one or in a block to perform data manipulation.
4. Close: After data manipulation, close the cursor explicitly.
5. Deallocate: Finally, delete the cursor definition and release all the
system resources associated with the cursor.
Types of Cursors in PL/SQL
Cursors are classified depending on the circumstances in which they are
opened.
 Implicit Cursor: If the Oracle engine opened a cursor for its internal
processing it is known as an Implicit Cursor. It is created
“automatically” for the user by Oracle when a query is executed and
is simpler to code.
 Explicit Cursor: A Cursor can also be opened for processing data
through a PL/SQL block, on demand. Such a user-defined cursor is
known as an Explicit Cursor.

Stored Procedures:
A stored procedure is a PL/SQL code block that has been compiled and
stored in one of the oracle engine's system tables. Before the procedure is
stored, the Oracle engine parses and compiles the procedure. If an error
occurs during the creation of the procedure, an invalid procedure gets
created. After creation, the oracle engine displays the errors. To display the
errors occurred in the procedure, use the SHOW_ERRORS command at the
SQL prompt. After storing it in the database, it can be called by another
application or PL/SQL subprograms. To create the stored procedure, you
must have the CREATE PROCEDURE privilege. Following syntax is used for
creating a Stored Procedures.
Syntax for creating stored procedure:
1. CREATE [OR REPLACE] PROCEDURE procedure_name
2. [ (parameter {IN|OUT|INOUT} datatype,........)]{IS|AS}
3. [local_declaration_section]
4. BEGIN
5. Executable_section
6. [EXCEPTION
7. exception_section]
8. END [procedure_name];
In the above syntax, the replace clause is required when you are trying to
recreate a procedure with the same name that already exists. It also drops
the existing procedure and replaces it with the new version created by the
statement. All the privileges granted to it and retained even if the new
replaced version performs a completely different task.
Following are the various examples of Stored Procedures:
Example 1: Create a stored procedure that accepts two numbers and
displays the product of two numbers?
1. CREATE OR REPLACE PROCEDURE product ( a number, b number)
AS c number;
2. BEGIN
3. c := a * b;
4. dbms_output.put_line ('the product is' || c);
5. END product;
6. /
Output:
Procedure created.
Explanation:
In the above PL/SQL program, on execution it will create a stored
Procedure named product. If any error occurs, then it can be viewed using
the SHOW ERRORS command written at SQL prompt. You can execute the
stored procedure using the EXECUTE command within SQL*PLUS
environment.
Example 2: Create a PL/SQL program to call a stored procedure
created below?
1. DECLARE
2. a number;
3. b number;
4. BEGIN
5. a := &a;
6. b := &b;
7. Product (a, b);
8. END
9. /
Output:
Procedure created.
Explanation:
In the above PL/SQL program, the execution of the stored procedure is to
call the procedure from within a PL/SQL block.

You might also like