You are on page 1of 6

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.

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


SQL is a single query that is used to to write the entire program blocks/
perform DML and DDL operations. procedure/ function, etc.

It is declarative, that defines what


needs to be done, rather than how PL/SQL is procedural that defines
things need to be done. how the things needs 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.
Conditional Expressions:
Following are Conditional Expressions in SQL

1. The CASE Expression: Let you use IF-THEN-ELSE statements without


having to invoke procedures.
In a simple CASE expression, the SQL searches for the first WHEN……
THEN pair for which expr is equal to comparison_expr and returns
return_expr. If above condition is not satisfied, an ELSE clause exists, the
SQL returns else_expr. Otherwise, returns NULL.
We cannot specify literal null for the return_expr and the else_expr. All of
the expressions(expr, comparison_expr, return_expr) must be of the
same data type.
Syntax:
2. CASE expr WHEN comparison_expr1 THEN return_expr1
3. [WHEN comparison_expr2 THEN return_expr2
4. .
5. .
6. .
7. WHEN comparison_exprn THEN return_exprn
8. ELSE else_expr]
END

The DECODE Function : Facilitates conditional inquiries by doing the work


of a CASE or IF-THEN-ELSE statement.
The DECODE function decodes an expression in a way similar to the IF-
THEN-ELSE logic used in various languages. The DECODE function
decodes expression after comparing it to each search value. If the
expression is the same as search, result is returned.
If the default value is omitted, a null value is returned where a search value
does not match any of the result values.
Syntax:
DECODE(col/expression, search1, result1
[, search2, result2,........,]
[, default])

COALESCE : Returns the first non-null argument. Null is returned only if all
arguments are null. It is often used to substitute a default value for null
values when data is retrieved for display.
NOTE: Same as CASE expressions, COALESCE also will not evaluate the
arguments to the right of the first non-null argument found.
Syntax:
COALESCE(value [, ......] )
GREATEST: Returns the largest value from a list of any number of
expressions. Comparison is case sensitive. If datatypes of all the
expressions in the list are not same, rest all expressions are converted to the
datatype of the first expression for comparison and if this conversion is not
possible, SQL will throw an error.
NOTE: Returns null if any expression in the list is null.
Syntax:
GREATEST(expr1, expr2 [, .....] )

IFNULL: If expr1 is not NULL, returns expr1; otherwise it returns expr2.


Returns a numeric or string value, depending on the context in which it is
used.
Syntax:
IFNULL(expr1, expr2)

IN: Checks whether a value is present within a set of values and can be used
with WHERE, CHECK and creation of views.
NOTE: Same as CASE and COALESCE expressions, IN also will not
evaluate the arguments to the right of the first non-null argument found.
Syntax:
WHERE column IN (x1, x2, x3 [,......] )

LEAST: Returns the smallest value from a list of any number of expressions.


Comparison is case sensitive. If datatypes of all the expressions in the list
are not same, rest all expressions are converted to the datatype of the first
expression for comparison and if this conversion is not possible, SQL will
throw an error.
NOTE: Returns null if any expression in the list is null.
Syntax:
LEAST(expr1, expr2 [, ......])

NULLIF: Returns a null value if value1=value2, otherwise it returns value1.


Syntax:
NULLIF(value1, value2)
Example:

Input:
SELECT NULLIF(9995463931, contact_num)
from Employee;
Loop statement:
In SQL Server, a loop is the technique where a set of SQL
statements are executed repeatedly until a condition is met.

SQL Server supports the WHILE loop. The execution of the


statements can be controlled from within the WHLE block using
BREAK and CONTINUE keywords.

Syntax:
WHILE <condition>
SQL Statement | statement_block | BREAK | CONTINUE

Boolean_expression : A boolean expression that returns TRUE or


FALSE.

sql_statement | statement_block : A single or a group of SQL


statements (statement block). Statement block should be enclosed
with BEGIN and END keywords.

BREAK: Causes the flow to exit from the innermost WHILE loop.
Statements after the END keyword are executed after the BREAK.

CONTINUE: Causes the WHILE loop to restart. It ignores all


statements after the CONTINUE keyword.

Select statement:
The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.

SELECT Syntax
SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the field names of the table you want to
select data from. If you want to select all the fields available in the table, use
the following syntax:

SELECT * FROM table_name;
The SELECT statement is probably the most important SQL command. It’s used to return
results from our database(s) and no matter how easy that could sound, it could be really
very complex. In this article, we’ll give an intro to the SELECT statement and we’ll cover
more complex stuff in the upcoming articles.

Motivation
In this series, we had 4 articles so far and we’ve created a simple database, populated it with
some data and explained what are primary and foreign keys. These are prerequisites needed to
start “playing” with our data:

In real-life situations, you’ll probably won’t insert all the data into the database. Data shall be
either inserted manually by multiple users of your application/system or by some automated
process(es). In these cases, you’ll either:

 Analyze data, most probably by using SELECT statements


 Track system performance
 Make changes in the data model to support new features

You might also like