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

RDBMS Unit 2

The document discusses PL/SQL including what it is, its block structure, variables, constants, literals, control structures like IF/ELSE and CASE statements. PL/SQL is a procedural language extension of SQL used to enhance the capabilities of SQL. It allows programming with blocks of code which can include variable declaration, condition checking and looping.

Uploaded by

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

RDBMS Unit 2

The document discusses PL/SQL including what it is, its block structure, variables, constants, literals, control structures like IF/ELSE and CASE statements. PL/SQL is a procedural language extension of SQL used to enhance the capabilities of SQL. It allows programming with blocks of code which can include variable declaration, condition checking and looping.

Uploaded by

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

UNIT-II

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.

PL/SQL block structure:


DECLARE

Declaration statements ;

BEGIN

Execution statements ;

EXCEPTION

Exception handling statements ;

END ;/

PL/SQL Block sections:


1. Declaration section (optional). 2. Execution section (mandatory). 3.
Exception handling section (optional).

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.

PL/SQL variables naming rules:


A variable name can’t contain more than 30 characters. A variable name must
start with an ASCII letter followed by any number, underscore (_) or dollar
sign ($). PL/SQL is case-insensitive i.e. var and VAR refer to the same variable.

How to declare variable in PL/SQL:


We have to declare a PL/SQL variable in the declaration section or in a
package as a global variable. After declaration PL/SQL allocates memory for
the variable and variable name is used to identify the storage location.

2
Syntax:
variable_name [ CONSTANT ] datatype [ NOT NULL] [:= | DEFAULT

initial_value ]

Where: variable_name is a valid identifier name. datatype is a valid PL/SQL


datatype.

Initializing Variables in PL/SQL:


When we declare a variable PL/SQL assigns it NULL as default value. If we
want to initialize a variable with a non-NULL value, we can do it during the
declaration. We can use any one of the following methods: 1. The DEFAULT
keyword Num1 binary_integer := 0;

2. The assignment operator siteName varchar2(20) DEFAULT ‘w3spoint’;

Example:
DECLARE

var1 integer := 20;

var2 integer := 40;

var3 integer ;

var4 real ;

BEGIN

var3 := var1 + var2 ;

dbms_output. put_line('Value of var3: ' || var3 );

var4 := 50.0/3.0;

dbms_output. put_line('Value of var4: ' || var4 );

3
END ;/

Output
Value of var3 : 60

Value of var4 :
16.66666666666666666666666666666666666667

Variable Scope in PL/SQL:


As we discussed in earlier tutorial that PL/SQL allows the nesting of blocks i.e.
blocks with blocks. Based on the nesting structure PL/SQL variables can be
divide into following categories: Local variables – Those variables which are
declared in an inner block and not accessible to outer blocks are known as
local variables. Global variables – Those variables which are declared in the
outer block or a package and accessible to itself and inner blocks are known as
global variables.

Example:
DECLARE

-- Global variables

num1 number := 10;

num2 number := 20;

BEGIN

dbms_output. put_line('Outer Variable num1: ' || num1 );

dbms_output. put_line('Outer Variable num2: ' || num2 );

DECLARE

-- Local variables

num3 number := 30;

num4 number := 40;


4
BEGIN

dbms_output. put_line('Outer variable in inner block num1: ' || num1 );

dbms_output. put_line('Outer variable in inner block num2: ' || num2 );

dbms_output. put_line('Inner Variable num3: ' || num3 );

dbms_output. put_line('Inner Variable num4: ' || num4 );

END ;

END ;/

Output:
Outer Variable num1 : 10

Outer Variable num2 : 20

Outer variable in inner block num1 : 10

Outer variable in inner block num2 : 20

Inner Variable num3 : 30

Inner Variable num4 : 40

Constants And Literals In Plsq


PL/SQL Constants:
A constant holds a value used in a PL/SQL block that does not change
throughout the program. It is a user-defined literal value.

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

pi constant number := 3.141592654;

-- other declarations

radius number (5 , 2);

dia number (5 , 2);

circumference number (7 , 2);

area number (10 , 2);

BEGIN

-- processing

radius := 10.5;

dia := radius * 2;

circumference := 2.0 * pi * radius ;

area := pi * radius * radius ;

-- output

dbms_output. put_line('Radius: ' || radius );

6
dbms_output. put_line('Diameter: ' || dia );

dbms_output. put_line('Circumference: ' || circumference );

dbms_output. put_line('Area: ' || area );

END ;/

Output:
Radius : 10.5

Diameter : 21

Circumference : 65.97Area: 346.36

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.

Types of literals in PL/SQL:


1. Numeric Literals (765, 23.56 etc.). 2. Character Literals (‘A’ ‘ 3. String
Literals (tutorialspointexamples.com etc.). 4. BOOLEAN Literals (TRUE, FALSE
and NULL). 5. Date and Time Literals (‘2016-12-25’ ‘2016-02-03 12:10:01’
etc.).

Hello World Plsql


pl sql hello world program:
DECLARE

-- variable declaration

message varchar2 (20):= 'Hello World!';


7
BEGIN

-- output

dbms_output. put_line( message );

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;

Block of statements1 executes when the specified condition is true.

IF-THEN-ELSE statement:
8
Syntax:

IF condition

THEN
//Block of statements1ELSE
//Block of statements2
END IF;

Block of statements1 executes when the specified condition is true otherwise


Block of statements2 executes.

IF-THEN-ELSIF statement:
Syntax:

IF condition1

THEN
//Block of statements1
ELSIF condition2
//Block of statements2ELSE
//Block of statements3
END IF;

Block of statements1 executes when condition1 is true if false codition2 is


checked and Block of statements2 executes if condition2 is true and so on.
Block of statements in ELSE block executes when no condition is true.

Example:
DECLARE

var number (3) := 50;

BEGIN

9
IF ( var = 10) THEN

dbms_output. put_line('Value of var is 10');

ELSIF ( var = 20) THEN

dbms_output. put_line('Value of var is 20');

ELSIF ( var = 30) THEN

dbms_output. put_line('Value of var is 30');


ELSE
dbms_output. put_line('None of the above condition is true.');

END IF;

dbms_output. put_line('Exact value of var is: '|| var );

END ;/

Output:

None of the above condition is true . Exact value of var is : 50

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

WHEN condition2 THEN Block of statements2

...

WHEN conditionn THEN Block of statementsn

ELSE Block of statements

END

Example:
DECLARE

nameChar char(1) := 'J';

BEGIN

CASE nameChar

when 'B' then dbms_output. put_line('Bharat');

when 'R' then dbms_output. put_line('Richi');

when 'S' then dbms_output. put_line('Sahdev');

when 'V' then dbms_output. put_line('Vinod');

when 'H' then dbms_output. put_line('Harish');

when 'M' then dbms_output. put_line('Mahesh');

when 'V' then dbms_output. put_line('Vivek');

when 'A' then dbms_output. put_line('Anil');

when 'J' then dbms_output. put_line('Jai');

else dbms_output. put_line('No such name');

END CASE;

END ;/

11
Output:
Jai

Exit Loop Plsql


Pl sql exit loop:
The pl sql loop repeatedly executes a block of statements until it reaches a
loop exit. The EXIT and EXIT WHEN statements are used to terminate a loop.

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.

PL/SQL LOOP statement syntax with EXIT:


LOOP
//block of statements
EXIT ;

END LOOP ;

PL/SQL LOOP statement syntax with EXIT


WHEN:
LOOP
//block of statements
EXIT WHEN condition ;

12
END LOOP ;

PL/SQL LOOP statement example with


EXIT:
DECLARE

num NUMBER := 1;

BEGIN

LOOP

DBMS_OUTPUT. PUT_LINE( num );

IF num = 10 THEN

EXIT ;

END IF;

num := num +1;

END LOOP ;

END ;

Output:
12345678910

PL/SQL LOOP statement example with


EXIT WHEN:
DECLARE

num NUMBER := 1;

BEGIN

13
LOOP

DBMS_OUTPUT. PUT_LINE( num );

EXIT WHEN num = 10;

num := num +1;

END LOOP ;

END ;

Output:
1
2
3
4
5
6
7
8
9
10

While Loop Plsql


PL SQL WHILE LOOP:
The pl sql while loop repeatedly executes a block of statements until a
particular condition is true. It first check the condition and executes a block of
statements if condition is true.

PL SQL WHILE LOOP syntax:

14
WHILE condition

LOOP
//block of statements;
END LOOP ;

PL SQL WHILE LOOP example:


DECLARE

num NUMBER := 1;

BEGIN

WHILE num <= 10

LOOP

DBMS_OUTPUT. PUT_LINE( num );

num := num +1;

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.

Pl sql for in loop syntax:


FOR loop_counter IN [ REVERSE ] start_value .. end_value

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.

Pl sql for in loop example:


DECLARE

BEGIN

FOR var IN 1 .. 10

LOOP

DBMS_OUTPUT. PUT_LINE( var );

END LOOP ;

END ;

16
Output:
12345678910

Pl sql for in loop reverse example:


DECLARE

BEGIN

FOR var IN REVERSE 1 .. 10

LOOP

DBMS_OUTPUT. PUT_LINE( var );

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.

Pl sql continue syntax:

continue;

Pl sql continue statement example:

DECLARE

num NUMBER := 0;

BEGIN

WHILE num < 10

LOOP

num := num +1;

IF num = 5 THEN
CONTINUE;
END IF;

DBMS_OUTPUT. PUT_LINE( num );

END LOOP ;

END ;

Output:
1
2
3
4
6

18
7
8
9
10

19

You might also like