0% found this document useful (0 votes)
4 views13 pages

Anathapindika DBMS Lab

This is DBMS LAb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Anathapindika DBMS Lab

This is DBMS LAb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

RDBMS Using PL/SQL Lab Record

(CSEB3552P)

BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE AND ENGINEERING

Submitted By:
Name: Anathapindika Dravichi
Roll No.: 12201220

Submitted to:
Dr. Gurjit Singh Bhathal
Assistant Professor

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


PUNJABI UNIVERSITY, PATIALA-147002 (INDIA)
September, 2024

1|Page
Contents

PRACTICAL-1: WAP to implement nested block in PL/SQL. ................................................ 1


PRACTICAL-2: WAP to implement scope of variables in PL/SQL. ...................................... 2
PRACTICAL-3: WAP to implement if condition in PL/SQL. ................................................. 3
PRACTICAL-4: WAP to implement if-else condition in PL/SQL. ......................................... 4
PRACTICAL-5: WAP to implement if-elsif-else condition in PL/SQL. .................................. 5
PRACTICAL-6: WAP to implement case condition in PL/SQL. ............................................. 6
PRACTICAL-7: WAP to implement loop statement in PL/SQL. ............................................. 7
PRACTICAL-8: WAP to implement for loop in PL/SQL. ....................................................... 8
PRACTICAL-9: WAP to implement while loop in PL/SQL. ................................................... 9
PRACTICAL-10: WAP to implement implicit cursor in PL/SQL. ......................................... 10
PRACTICAL-11: WAP to implement explicit cursor in PL/SQL. ......................................... 11
PRACTICAL-1: WAP to implement nested block in PL/SQL.

In PL/SQL, a nested block is a smaller block of code placed inside a larger block. This helps
organize code better and allows the inner block to use its own variables and handle its own
errors while still being part of the larger block.

SOLUTION:

DECLARE

x NUMBER := 10;

BEGIN

DECLARE

x NUMBER := 20;

BEGIN

DBMS_OUTPUT.PUT_LINE(x); -- Outputs 20

END;

DBMS_OUTPUT.PUT_LINE(x); -- Outputs 10

END;

OUTPUT:

1|Page
PRACTICAL-2: WAP to implement scope of variables in PL/SQL.

In PL/SQL, local variables are declared within a specific block or subprogram and are
accessible only within that block. Global variables, on the other hand, are declared in the
declaration section of a package or a standalone PL/SQL block and can be accessed by any
subprogram within the same package or session.

SOLUTION:

DECLARE

num1 NUMBER := 90;

num2 NUMBER := 80;

BEGIN

DBMS_OUTPUT.PUT_LINE('Outer variables num1: ' || num1);

DBMS_OUTPUT.PUT_LINE('Outer variables num2: ' || num2);

DECLARE

num1 NUMBER := 190;

num2 NUMBER := 180;

BEGIN

DBMS_OUTPUT.PUT_LINE('Inner variables num1: ' || num1);

DBMS_OUTPUT.PUT_LINE('Inner variables num2: ' || num2);

END;

END;

OUTPUT:
PRACTICAL-3: WAP to implement if condition in PL/SQL.

In PL/SQL, the IF condition is used to execute a block of code only when a specified condition
evaluates to true, enabling conditional logic within procedural code.

SOLUTION:

DECLARE

v_num NUMBER := 10;

BEGIN

IF v_num > 5 THEN

DBMS_OUTPUT.PUT_LINE('v_num is greater than 5');

END IF;

END;

OUTPUT:
PRACTICAL-4: WAP to implement if-else condition in PL/SQL.

In PL/SQL, the IF...ELSE statement is used to execute one block of code if a condition is true
and another block if the condition is false.

SOLUTION:

DECLARE
v_num NUMBER := 3;
BEGIN
IF v_num > 5 THEN
DBMS_OUTPUT.PUT_LINE('v_num is greater than 5');
ELSE
DBMS_OUTPUT.PUT_LINE('v_num is 5 or less');
END IF;
END;
/

OUTPUT:
PRACTICAL-5: WAP to implement if-elsif-else condition in PL/SQL.

In PL/SQL, ELSIF is used to specify additional conditions to be evaluated if the initial IF


condition is false, allowing for multiple conditional branches within a single IF statement.

SOLUTION:

DECLARE
v_num NUMBER := 5;
BEGIN
IF v_num > 5 THEN
DBMS_OUTPUT.PUT_LINE('v_num is greater than 5');
ELSIF v_num = 5 THEN
DBMS_OUTPUT.PUT_LINE('v_num is 5');
ELSE
DBMS_OUTPUT.PUT_LINE('v_num is 5 less than 5');
END IF;
END;
/

OUTPUT:
PRACTICAL-6: WAP to implement case condition in PL/SQL.

In PL/SQL, the CASE statement is used to handle multiple conditional branches by evaluating
an expression against a set of possible values and executing the corresponding block of code
for the first matching value.

SOLUTION:

DECLARE
v_day NUMBER := 3;
BEGIN
CASE v_day
WHEN 6 THEN
DBMS_OUTPUT.PUT_LINE('It is Saturday');
WHEN 7 THEN
DBMS_OUTPUT.PUT_LINE('It is Sunday');
ELSE
DBMS_OUTPUT.PUT_LINE('It is a weekday');
END CASE;
END;
/

OUTPUT:
PRACTICAL-7: WAP to implement loop statement in PL/SQL.

In PL/SQL, the LOOP statement repeatedly executes a block of code until an explicit EXIT
statement is encountered, allowing for iterative operations based on dynamic conditions.

SOLUTION:

DECLARE
v_counter NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('Counter: ' || v_counter);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 5;
END LOOP;
END;
/

OUTPUT:
PRACTICAL-8: WAP to implement for loop in PL/SQL.

In PL/SQL, a FOR loop automatically repeats a block of code a set number of times or through
a set of records, making it easy to work with sequences or cursor results.

SOLUTION:

BEGIN
FOR I IN 1..3 LOOP
DBMS_OUTPUT.PUT_LINE('Iteration: ' || i);
END LOOP;
END;
/

OUTPUT:
PRACTICAL-9: WAP to implement while loop in PL/SQL.

In PL/SQL, a WHILE loop keeps running a block of code as long as a condition is true, which
is useful when you don’t know in advance how many times you need to repeat the code.

SOLUTION:

DECLARE
v_counter NUMBER := 1;
BEGIN
WHILE v_counter <= 4 LOOP
DBMS_OUTPUT.PUT_LINE('No. ' || v_counter);
v_counter := v_counter + 1;
END LOOP;
END;
/

OUTPUT:
PRACTICAL-10: WAP to implement implicit cursor in PL/SQL.

In PL/SQL, an implicit cursor is a built-in tool that automatically handles SQL queries and
updates without needing you to set it up manually.

SOLUTION:

DECLARE
total_rows NUMBER(4);
BEGIN
-- Update the salary for all employees
UPDATE employees SET salary = salary - 500;
IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee updated');
ELSIF SQL%FOUND THEN
total_rows := SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(total_rows || ' employees updated');
END IF;
END;
/

OUTPUT:
PRACTICAL-11: WAP to implement explicit cursor in PL/SQL.

An explicit cursor in PL/SQL is a user-defined construct that allows programmers to declare,


open, fetch from, and close a cursor explicitly to manage the context of a query and process
multiple rows of data efficiently.

SOLUTION:

DECLARE
CURSOR emp_cursor IS SELECT emp_name FROM employees;
v_emp_name employees.emp_name%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_emp_name;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_emp_name);
END LOOP;
CLOSE emp_cursor;
END;
/

OUTPUT:

You might also like