RDBMS Lab
RDBMS Lab
(CSEB3552P)
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE AND ENGINEERING
Submitted By:
Name: Binod Kapadi
RollNo: 12201221
Submitted to:
Dr. Gurjit Singh Bhathal
Assistant Professor
CONTROL STRUCTURES:
Control structures in PL/SQL are constructs that allow you to control the flow of
execution in a program.
I) CONDITIONAL STATEMENTS:
SYNTAX:
IF condition THEN
-- Statements to execute if the condition is true
END IF;
OUTPUT =>
The number 30 is greater than 5.
B) IF-ELSE Statement
The IF-ELSE statement allows you to execute one block of code if the condition is
true and another block if the condition is false.
SYNTAX:
IF condition THEN
-- Statements to execute if the condition is true
ELSE
-- Statements to execute if the condition is false
END IF;
OUTPUT =>
C) IF-ELSIF-ELSE Statement
Evaluates multiple conditions in sequence, executing the block of code
corresponding to the first true condition. If none of the conditions are true, the code
block in the ELSE part is executed.
SYNTAX:
IF condition1 THEN
-- Statements to execute if condition1 is true
ELSIF condition2 THEN
-- Statements to execute if condition2 is true
ELSE
-- Statements to execute if none of the conditions is true
END IF;
D) CASE Statement:
Selects a block of code to execute based on the value of an expression.
SYNTAX:
CASE expression
WHEN value1 THEN
-- Statements to execute if expression equals value1
WHEN value2 THEN
-- Statements to execute if expression equals value2
...
ELSE
-- Statements to execute if no match is found
END CASE;
DECLARE
a NUMBER := 60; -- First number
b NUMBER := 20; -- Second number
op CHAR(1) := '/'; -- Operation (+, -, *, /)
result NUMBER; -- Variable to store the result
BEGIN
-- Using CASE statement to perform the operation
CASE op
WHEN '+' THEN
result := a + b;
WHEN '-' THEN
result := a - b;
WHEN '*' THEN
result := a * b;
WHEN '/' THEN
-- Checking for division by zero
IF b != 0 THEN
result := a / b;
ELSE
DBMS_OUTPUT.PUT_LINE('Error: Division by zero');
RETURN;
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('Invalid operation');
RETURN;
END CASE;
-- Display the result
DBMS_OUTPUT.PUT_LINE('The result is : ' || result);
END;
OUTPUT =>
The result is : 3
II) Iterative Control:
Iterative control in PL/SQL refers to structures that allow you to repeatedly execute
a block of code multiple times.
A) BASIC LOOP:
The BASIC LOOP repeatedly executes a block of code until an EXIT statement is
encountered.
SYNTAX:
LOOP
-- Statements to execute
EXIT WHEN condition;
END LOOP;
DECLARE
a NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('Counter: ' || a);
a := a + 1;
IF a > 7 THEN
EXIT; -- Exit the loop
END IF;
END LOOP;
END;
OUTPUT =>
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Counter: 6
Counter: 7
B) FOR LOOP:
The FOR LOOP executes a block of code a specific number of times, based on a
counter that automatically increments or decrements.
SYNTAX:
REVERSE: Optional keyword that causes the loop to count down instead of up.
Specifies the range for the loop counter.
lower_bound .. upper_bound:
BEGIN
FOR a IN 0..8 LOOP
DBMS_OUTPUT.PUT_LINE('Iteration: ' || a);
END LOOP;
END;
OUTPUT =>
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 7
Iteration: 8
C) WHILE LOOP:
The WHILE LOOP executes a block of code as long as a specified condition is true.
SYNTAX:
DECLARE
a NUMBER:= 0;
BEGIN
WHILE a <= 5
LOOP
dbms_output.put_line('Counter: ' || a);
a := a + 1;
END LOOP;
END;
OUTPUT =>
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
DECLARE
-- Outer block declarations
BEGIN
DECLARE
-- Inner block declarations
BEGIN
-- Access to only inner block variables
END;
-- Access to only outer block variables
END;
8) WAP to evaluate NESTED BLOCKS in PL/SQL.
DECLARE
a NUMBER := 50;
BEGIN
DECLARE
a NUMBER := 70;
BEGIN
DBMS_OUTPUT.PUT_LINE(a); -- Outputs 70
END;
DBMS_OUTPUT.PUT_LINE(a); -- Outputs 50
END;
OUTPUT =>
70
50
LOCAL AND GLOBAL VARIABLES IN PL/SQL:
Local variables are declared within a PL/SQL block (e.g., a BEGIN...END block) and
are only accessible within that block and any nested blocks. They are created and
destroyed when the block executes.
Global variables in PL/SQL are those declared outside of any PL/SQL blocks, typically
at the package level. These variables are accessible throught the program.
DECLARE
--Global variables
a NUMBER := 60;
b NUMBER := 70;
BEGIN
DBMS_OUTPUT.PUT_LINE('Outer variables a: ' || a);
DBMS_OUTPUT.PUT_LINE('Outer variables b: ' || b);
DECLARE
--Local variables
a NUMBER := 150;
b NUMBER := 130;
BEGIN
DBMS_OUTPUT.PUT_LINE('Inner variables a: ' || a);
DBMS_OUTPUT.PUT_LINE('Inner variables b: ' || b);
END;
END;
OUTPUT =>
Outer variables a: 60
Outer variables b: 70
Inner variables a: 150
Inner variables b: 130
CURSOR:
In PL/SQL, a cursor is a database object used to retrieve and manipulate data row-
by-row. Cursors are essential for handling SQL query results and performing
operations on the fetched data. Using cursors effectively allows you to handle
complex data retrieval and processing tasks in PL/SQL.
A) IMPLICIT CURSOR:
Typically, you use implicit cursors for simple operations where you don't need
Usage:
to manually control the cursor.
DECLARE
total_rows number(4);
BEGIN
UPDATE employees
IF sql%rowcount = 0 THEN
ELSE
total_rows := sql%rowcount;
END IF;
END;
OUTPUT =>
4 employees updated
B) EXPLICIT CURSOR:
Explicit cursors are defined and managed by the programmer. They are used when
you need to handle multiple rows returned by a query and process each row
individually.
DECLARE
v_emp_name employees.emp_name%TYPE;
BEGIN
LOOP
EXIT WHEN emp_cursor%NOTFOUND; -- Exit loop when no more rows are fetched
END LOOP;
END;
Assuming the employees table has the following data:
emp_name
Binod Kapadi
Ruby Singh
Rohan Joshi
Aashish Sha
Binod Kapadi
Ruby Singh
Rohan Joshi
Aashish Sha
PROCEDURES:
BEGIN
greet_person('Binod');
END;
OR
-- Execute the procedure using EXECUTE command
Execute greet_person('Binod');
OUTPUT =>
Hello, Binod!