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

RDBMS Lab

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

RDBMS Lab

Rdbms Lab
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

RDBMS Using PL/SQL Lab Record

(CSEB3552P)

BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE AND ENGINEERING

Submitted By:
Name: Binod Kapadi
RollNo: 12201221

Submitted to:
Dr. Gurjit Singh Bhathal
Assistant Professor

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


PUNJABI UNIVERSITY, PATIALA-147002 (INDIA)
September, 2024
Contents:
PRACTICAL-1: WAP to implement IF Condition in PL/SQL.

PRACTICAL-2: WAP to implement IF_ELSIF Condition in PL/SQL.

PRACTICAL-3: WAP to implement IF_ELSIF_ELSE Condition in PL/SQL.

PRACTICAL-4: WAP to implement CASE Statement in PL/SQL.

PRACTICAL-5: WAP to implement BASIC Loop in PL/SQL.

PRACTICAL-6: WAP to implement FOR Loop in PL/SQL.

PRACTICAL-7: WAP to implement WHILE Loop in PL/SQL.

PRACTICAL-8: WAP to implement NESTED Blocks in PL/SQL.

PRACTICAL-9: WAP to implement Scope Of Variable in PL/SQL.

PRACTICAL-10: WAP to implement IMPLICIT Cursor in PL/SQL.

PRACTICAL-11: WAP to implement EXPLICIT Cursor in PL/SQL.

PRACTICAL-12: WAP to implement PROCEDURES in PL/SQL.

CONTROL STRUCTURES:
Control structures in PL/SQL are constructs that allow you to control the flow of
execution in a program.
I) CONDITIONAL STATEMENTS:

Conditional statements allow a program to execute certain blocks of code based on


whether a specified condition is true or false.
A) IF CONDITION:

Executes a block of code if a condition is true.

SYNTAX:

IF condition THEN
-- Statements to execute if the condition is true
END IF;

1) WAP TO EVALUATE IF STATEMENT IN PL/SQL.


DECLARE
a number := 30;
BEGIN
if a>5 then
dbms_output.put_line('The number ' || a || ' is greater than 5.');
END IF;
END;

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;

2) WAP TO EVALUATE IF_ELSE STATEMENT IN PL/SQL .


DECLARE
a NUMBER := 30;
BEGIN
IF a>15 then
dbms_output.put_line('The number ' || a || ' is greater than 5');
ELSE
dbms_output.put_line('The number ' || a || 'is less than or equal to 5.');
END IF;
END;

OUTPUT =>

The number 10 is less than or equal to 15.

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;

3) WAP TO EVALUATE IF_ELSIF_ELSE STATEMENT IN PL/SQL.


DECLARE
a NUMBER := 15; -- You can change these values as needed
b NUMBER := 45;
c NUMBER := 30;
greatest NUMBER;
BEGIN
IF a >= b AND a >= c THEN
greatest := a;
ELSIF b >= a AND b >= c THEN
greatest := b;
ELSE
greatest := c;
END IF;
DBMS_OUTPUT.PUT_LINE('The greatest number is: ' || greatest);
END;
OUTPUT =>

The greatest number is: 45

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;

4) WAP to evaluate CASE Statement in PL/SQL.

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;

5) WAP to evaluate BASIC LOOP in PL/SQL.

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:

FOR counter IN REVERSE lower_bound .. upper_bound LOOP


-- Statements to execute
END LOOP;

 REVERSE: Optional keyword that causes the loop to count down instead of up.
 Specifies the range for the loop counter.
lower_bound .. upper_bound:

6) WAP to evaluate FOR LOOP in PL/SQL.

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:

WHILE condition LOOP


-- Statements to execute
END LOOP;

7) WAP to evaluate WHILE LOOP in PL/SQL.

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

NESTED BLOCKS INPL/SQL:


In PL/SQL, you can create nested blocks, where one block of code is placed inside
another. This is useful for organizing code, managing variables with different
scopes, and handling exceptions specific to inner blocks.
Scope: Variables declared in the outer block are accessible in the inner block, but
variables declared in the inner block are not accessible in the outer block.
SYNTAX:

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.

9) WAP to evaluate Scope Of Variables in PL/SQL.

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:

Implicit cursors are automatically created by Oracle when a SQL statement is


executed that does not explicitly use a cursor. They are used for single-row queries,
DML operations (INSERT, UPDATE, DELETE), and certain SELECT statements.

Typically, you use implicit cursors for simple operations where you don't need
Usage:
to manually control the cursor.

10) WAP to evaluate IMPLICIT CURSOR.

DECLARE

total_rows number(4);

BEGIN

-- Update salaries and store the number of rows affected

UPDATE employees

SET salary = salary - 500;

-- Check if any rows were affected

IF sql%rowcount = 0 THEN

dbms_output.put_line('No employee selected');

ELSE

total_rows := sql%rowcount;

dbms_output.put_line(total_rows ||' employees selected');

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.

Steps to Use Explicit Cursors:

Declare the cursor.


Open the cursor to establish the result set.
Fetch rows from the cursor into variables.
Close the cursor when done.

11) WAP to evaluate EXPLICIT CURSOR.

DECLARE

CURSOR emp_cursor IS SELECT emp_name FROM employees; -- Cursor


Declaration

v_emp_name employees.emp_name%TYPE;

BEGIN

OPEN emp_cursor; -- Open the cursor

LOOP

FETCH emp_cursor INTO v_emp_name; -- Fetch a row

EXIT WHEN emp_cursor%NOTFOUND; -- Exit loop when no more rows are fetched

DBMS_OUTPUT.PUT_LINE(v_emp_name); -- Output the employee name

END LOOP;

CLOSE emp_cursor; -- Close the cursor

END;
Assuming the employees table has the following data:

emp_name
Binod Kapadi
Ruby Singh
Rohan Joshi
Aashish Sha

The output of the PL/SQL block would be:

Binod Kapadi
Ruby Singh
Rohan Joshi
Aashish Sha
PROCEDURES:

The PL/SQL stored procedure or simply a procedure is a PL/SQL block which


performs one or more specific tasks. Procedures can take parameters, execute SQL
statements, and encapsulate business logic, making them reusable and modular.
They help in organizing code, reducing redundancy, and improving maintainability.

12) WAP to implement PROCEDURES in PL/SQL.


-- Create or replace the procedure

CREATE OR REPLACE PROCEDURE greet_person(p_username IN VARCHAR2) IS


BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, '|| p_username || '!');
END greet_person;

-- Call the procedure within an anonymous PL/SQL block

BEGIN
greet_person('Binod');
END;

OR
-- Execute the procedure using EXECUTE command

Execute greet_person('Binod');

OUTPUT =>

Hello, Binod!

You might also like