13_July_2024
13_July_2024
In this practice, you use PL/SQL code to interact with the Oracle Server.
1.
Create a PL/SQL block that selects the maximum department ID in the departments table
and stores it in the v_max_deptno variable. Display the maximum department ID.
DECLARE
v_max_deptno NUMBER ;
BEGIN
SELECT MAX(department_id)
INTO v_max_deptno
FROM departments ;
DBMS_OUTPUT.PUT_LINE('The maximum department_id is : ' ||v_max_deptno) ;
END ;
/
DECLARE
v_max_deptno NUMBER ;
v_dept_name departments.department_name%TYPE := 'Education' ;
v_dept_id NUMBER ;
BEGIN
SELECT MAX(department_id)
INTO v_max_deptno
FROM departments ;
DBMS_OUTPUT.PUT_LINE('The maximum department_id is : ' ||v_max_deptno) ;
v_dept_id := 10 + v_max_deptno ;
Page 1 of 4
Youtube Channel: https://www.youtube.com/@muhammadnurealam
Teacher: Muhammad Nur E Alam, nurealam.rajjak@gmail.com WhatsApp: +8801917975575
BEGIN
UPDATE departments
SET location_id = 3000
WHERE department_id = 280 ;
END ;
/
1.
Execute the command in the lab_06_01.sql file to create the messages table. Write a
PL/SQL block to insert numbers into the messages table.
a. Insert the numbers 1 through 10, excluding 6 and 8.
b. Commit before the end of the block.
BEGIN
FOR i in 1..10
LOOP
IF i = 6 or i = 8 THEN
null ;
ELSE
INSERT INTO messages(results)
VALUES (i) ;
END IF ;
END LOOP ;
COMMIT ;
END ;
/
Page 2 of 4
Youtube Channel: https://www.youtube.com/@muhammadnurealam
Teacher: Muhammad Nur E Alam, nurealam.rajjak@gmail.com WhatsApp: +8801917975575
2.
Execute the lab_06_02.sql script. This script creates an emp table that is a replica of the
employees table. It alters the emp table to add a new column, stars, of VARCHAR2 data
type and size 50. Create a PL/SQL block that inserts an asterisk in the stars column for
every $1000 of the employee’s salary. Save your script as lab_06_02_soln.sql.
Ans:
====
SALARY NVL(ROUND(SALARY/1000),0)
---------- -------------------------
8600 9
Page 3 of 4
Youtube Channel: https://www.youtube.com/@muhammadnurealam
Teacher: Muhammad Nur E Alam, nurealam.rajjak@gmail.com WhatsApp: +8801917975575
DECLARE
v_empno emp.employee_id%TYPE := 176 ;
v_asterisk emp.stars%TYPE := NULL ;
v_sal emp.salary%TYPE ;
BEGIN
SELECT NVL(ROUND(salary/1000), 0)
INTO v_sal
FROM emp
WHERE employee_id = v_empno ;
FOR i IN 1..v_sal
LOOP
v_asterisk := v_asterisk ||'*' ;
END LOOP ;
UPDATE emp
SET stars = v_asterisk
WHERE employee_id = v_empno ;
COMMIT;
END;
/
SALARY RS STARS
---------- ---------- --------------------------
8600 9 *********
==========================
Page 4 of 4
Youtube Channel: https://www.youtube.com/@muhammadnurealam