RDBMS Unit 2
RDBMS Unit 2
PL/SQL
What is PL/SQL:
PL/SQL stands for Procedural Language extension of SQL. It was developed by
Oracle Corporation in the late 1980s to enhance the capabilities of SQL. It is
the procedural extension language for SQL.
Declaration statements ;
BEGIN
Execution statements ;
EXCEPTION
END ;/
Declaration section:
It is an optional section and starts with DECLARE keyword. It is used to
declare the variables, constants, records and cursors etc.
Execution section:
Execution section starts with BEGIN keyword and ends with END keyword. It is
a mandatory section. It is used to write the program logic code. Note:
Execution section must have one statement.
1
Exception handling section:
Execution section starts with EXCEPTION keyword. It is an optional section. It
is used to handle the exceptions occurred in execution section.
Important points:
1. Every PL/SQL statement will be followed by semicolon (;). 2. PL/SQL blocks
can be nested.
Advantages of PL/SQL:
1. PL/SQL is a procedural language. 2. PL/SQL is a block structure language. 3.
PL/SQL handles the exceptions. 4. PL/SQL engine can process the multiple SQL
statements simultaneously as a single block hence reduce network traffic and
provides better performance.
Variables In Plsql
Variable:
Variable is the name of reserved memory location. Each variable has a specific
data type which determines the range of values and set of operations for that
variable.
2
Syntax:
variable_name [ CONSTANT ] datatype [ NOT NULL] [:= | DEFAULT
initial_value ]
Example:
DECLARE
var3 integer ;
var4 real ;
BEGIN
var4 := 50.0/3.0;
3
END ;/
Output
Value of var3 : 60
Value of var4 :
16.66666666666666666666666666666666666667
Example:
DECLARE
-- Global variables
BEGIN
DECLARE
-- Local variables
END ;
END ;/
Output:
Outer Variable num1 : 10
5
Syntax to declare a constant:
constant_name CONSTANT datatype := VALUE ;
Where:
constant_name – is a valid identifier name. CONSTANT – is a
keyword. VALUE – is a value which must be assigned to a constant when it is
declared. You cannot assign a value later.
Example:
DECLARE
-- constant declaration
-- other declarations
BEGIN
-- processing
radius := 10.5;
dia := radius * 2;
-- output
6
dbms_output. put_line('Diameter: ' || dia );
END ;/
Output:
Radius : 10.5
Diameter : 21
PL/SQL Literals:
Literals is an explicit numeric, character, string or Boolean values which are
not represented by identifiers i.e. TRUE, NULL, w3spoint etc. Note: PL/SQL
literals are case-sensitive.
-- variable declaration
-- output
END ;/
Output:
Hello World !
If Else Plsql
PL/SQL If statement:
If statement is used to execute a block of statements if specified condition is
true. Commonly used PL/SQL If statement:
IF-THEN statement:
Syntax:
IF condition
THEN
//Block of statements1
END IF;
IF-THEN-ELSE statement:
8
Syntax:
IF condition
THEN
//Block of statements1ELSE
//Block of statements2
END IF;
IF-THEN-ELSIF statement:
Syntax:
IF condition1
THEN
//Block of statements1
ELSIF condition2
//Block of statements2ELSE
//Block of statements3
END IF;
Example:
DECLARE
BEGIN
9
IF ( var = 10) THEN
END IF;
END ;/
Output:
Switch Plsql
PL/SQL Case Statement:
Switch statement is used to execute a block of statement based on the switch
expression value. An expression must be of type int, short, byte or char. A
case value should be a constant literal value and cannot be duplicated.
Expression value is compared with each case value. If a match found
corresponding block of statements will be executed. A break statement is used
to terminate the execution of statements. If no case value matches with
expression value then default block of statements will be executed. If break
statement is not used within case, all matching cases will be executed. Syntax:
CASE [ expression ]
10
WHEN condition1 THEN Block of statements1
...
END
Example:
DECLARE
BEGIN
CASE nameChar
END CASE;
END ;/
11
Output:
Jai
Where:
EXIT: The EXIT statement is used to terminate the loop unconditionally and
normally used with IF statement. EXIT WHEN: The EXIT WHEN statement is
used to terminate the loop conditionally. It terminates the loop when the
specified condition is true.
END LOOP ;
12
END LOOP ;
num NUMBER := 1;
BEGIN
LOOP
IF num = 10 THEN
EXIT ;
END IF;
END LOOP ;
END ;
Output:
12345678910
num NUMBER := 1;
BEGIN
13
LOOP
END LOOP ;
END ;
Output:
1
2
3
4
5
6
7
8
9
10
14
WHILE condition
LOOP
//block of statements;
END LOOP ;
num NUMBER := 1;
BEGIN
LOOP
END LOOP ;
END ;
Output:
1
2
3
4
5
6
7
8
9
10
15
For In Loop Plsql
Pl sql for in loop:
The pl sql for in loop repeatedly executes a block of statements for a fixed
number of times. The loop iteration occurs between the start and end integer
values. The counter is always incremented by 1 and loop terminates when the
counter reaches the value of the end integer.
LOOP
//block of statements.
END LOOP ;
Note: 1. The double dot (..) specifies the range operator. 2. By default
iteration is from start_value to end_value but we can reverse the iteration
process by using REVERSE keyword. 3. No need to declare the counter
variable explicitly because it is declared implicitly in the declaration section. 4.
The counter variable is incremented by 1 and does not need to be incremented
explicitly. 5. The EXIT and EXIT WHEN statements can be used.
BEGIN
FOR var IN 1 .. 10
LOOP
END LOOP ;
END ;
16
Output:
12345678910
BEGIN
LOOP
END LOOP ;
END ;
Output :
10
9
8
7
6
5
4
3
2
1
Continue Plsql
Pl sql continue statement:
17
The pl sql continue statement is a control statement which is used to skip the
following statement in the body of the loop and continue with the next
iteration of the loop.
continue;
DECLARE
num NUMBER := 0;
BEGIN
LOOP
IF num = 5 THEN
CONTINUE;
END IF;
END LOOP ;
END ;
Output:
1
2
3
4
6
18
7
8
9
10
19