Anathapindika DBMS Lab
Anathapindika DBMS Lab
(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
1|Page
Contents
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
BEGIN
DECLARE
BEGIN
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
BEGIN
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.
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.
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: