Plsql1
Plsql1
PL/SQL, which stands for Procedural Language extensions to the Structured Query Language (SQL). It is a
combination of SQL along with the procedural features of programming languages. It was developed by Oracle
Corporation in the early 90's to enhance the capabilities of SQL. PL/SQL is one of three key programming
languages embedded in the Oracle Database, along with SQL itself and Java.
Purpose of PL/SQL
The purpose of PL/SQL is to merge database commands with procedural programming language. It offers
more complete programming solutions for building critical applications that operate on the Oracle database.
Features of PL/SQL
Learning PL/SQL is an essential skill for persons who are interested in databases and other advanced RDBMS
technologies. PL/SQL offers various benefits, making it an essential skill for database developers −
Ease of Use: PL/SQL is straightforward to write and read, featuring block-structured syntax which
simplifies programming and debugging.
Portability: Programs written in PL/SQL are fully portable across different Oracle databases, ensuring
consistency and ease of migration.
Tight SQL Integration: PL/SQL is tightly integrated with SQL, allowing for efficient querying,
transforming, and updating of data within a database.
High Performance: It reduces network traffic by sending entire blocks of statements to the database
at once, thus improving performance.
Security: It includes robust security features to protect database integrity.
Object-Oriented Support: It supports object-oriented programming, and allows you to define object
types that can be used in object-oriented designs.
PL/SQL follows a block-structured approach, dividing programs into logical blocks of code. Each block consists
of three main sections −
Declarations: This section, starting with the keyword DECLARE, is optional and used for
defining variables, cursors, subprograms, and other elements required within the block.
Executable Commands: Enclosed between the keywords BEGIN and END, this mandatory section
contains executable PL/SQL statements. It must include at least one executable line of code, even if
it's just a NULL command indicating no action.
Exception Handling: This starts with the keyword EXCEPTION, this optional section deals with
handling errors in the program through defined exceptions.
PL/SQL statements are terminated with a semicolon(;). Additionally, blocks can be nested within each other
using BEGIN and END keywords.
Applications of PL/SQL
Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other PL/SQL blocks
using BEG
PL/SQL Delimiters
PL/SQL Comments
Program comments are explanatory statements that can be included in the PL/SQL code that you write and
helps anyone reading its source code. All programming languages allow some form of comments.
The PL/SQL supports single-line and multi-line comments. All characters available inside any comment are
ignored by the PL/SQL compiler. The PL/SQL single-line comments start with the delimiter -- (double hyphen)
and multi-line comments are enclosed by /* and */.
A constant is declared using the CONSTANT keyword. It requires an initial value and does not allow that value
to be changed.
PL/SQL – Operators
PL/SQL - Conditions
PL/SQL Loop
PL/SQL - Strings
Fixed-length strings − In such strings, programmers specify the length while declaring the string. The
string is right-padded with spaces to the length so specified.
Variable-length strings − In such strings, a maximum length up to 32,767, for the string is specified
and no padding takes place.
Character large objects (CLOBs) − These are variable-length strings that can be up to 128 terabytes.
PL/SQL – Arrays
Creating a Varray Type
A varray type is created with the CREATE TYPE statement. You must specify the maximum size and the type of elements
stored in the varray.
The basic syntax for creating a VARRAY type at the schema level is −
Example
Output
PL/SQL - Procedures
A subprogram is a program unit/module that performs a particular task. These subprograms are combined to
form larger programs. This is basically called the 'Modular design'. A subprogram can be invoked by another
subprogram or program which is called the calling program.
Functions − These subprograms return a single value; mainly used to compute and return a value.
Procedures − These subprograms do not return a value directly; mainly used to perform an action.
Creating a Procedure
A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The simplified syntax for the
CREATE OR REPLACE PROCEDURE statement is as follows −
Positional notation
Named notation
Mixed notation
Positional Notation
Named Notation
In named notation, the actual parameter is associated with the formal parameter using the arrow symbol (
=>). The procedure call will be like the following −
Mixed Notation
In mixed notation, you can mix both notations in procedure call; however, the positional notation should
precede the named notation.
Creating a Function
A standalone function is created using the CREATE FUNCTION statement. The simplified syntax for the CREATE
OR REPLACE PROCEDURE statement is as follows −
Output
Calling a Function
Example
The following example demonstrates Declaring, Defining, and Invoking a Simple PL/SQL Function that
computes and returns the maximum of two values.
We have seen that a program or subprogram may call another subprogram. When a subprogram calls itself, it
is referred to as a recursive call and the process is known as recursion.
Arithmetic operator
Relational Operators
Logical Operators
Assignment Operators
Creating of Table
Conditional statements
IF Statement with Multiple Conditions
CASE Statement
Nested IF Statement
Using Conditions in Loops
FOR Loop
WHILE LOOP
LOOP with EXIT Condition
Nested Loops
ARRAYS