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

13_July_2024

The document provides PL/SQL code examples for interacting with an Oracle Server, including selecting, inserting, updating, and deleting records in a departments table and a messages table. It demonstrates the use of control structures to manage data and includes specific tasks such as inserting numbers while excluding certain values and updating a new column based on employee salaries. Additionally, it outlines the creation of tables and the execution of SQL commands within PL/SQL blocks.

Uploaded by

mdtuhinalom05
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)
1 views

13_July_2024

The document provides PL/SQL code examples for interacting with an Oracle Server, including selecting, inserting, updating, and deleting records in a departments table and a messages table. It demonstrates the use of control structures to manage data and includes specific tasks such as inserting numbers while excluding certain values and updating a new column based on employee salaries. Additionally, it outlines the creation of tables and the execution of SQL commands within PL/SQL blocks.

Uploaded by

mdtuhinalom05
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/ 4

Teacher: Muhammad Nur E Alam, nurealam.rajjak@gmail.

com WhatsApp: +8801917975575

Solution 5: Using SQL Statements within a PL/SQL


@@@@@@@@@@@@@@@@@@@@@@@

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.

a. Declare a variable v_max_deptno of type NUMBER in the declarative section.

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 ;
/

The maximum department_id is : 270

v_dept_name departments.department_name%TYPE:= 'Education';


v_dept_id NUMBER;

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 ;

INSERT INTO departments (department_id, department_name, location_id)


VALUES (v_dept_id, v_dept_name, NULL) ;
DBMS_OUTPUT.PUT_LINE (' SQL%ROWCOUNT gives ' || SQL%ROWCOUNT) ;
END ;
/

Page 1 of 4
Youtube Channel: https://www.youtube.com/@muhammadnurealam
Teacher: Muhammad Nur E Alam, nurealam.rajjak@gmail.com WhatsApp: +8801917975575

SELECT * FROM departments WHERE department_id= 280 ;

BEGIN
UPDATE departments
SET location_id = 3000
WHERE department_id = 280 ;
END ;
/

SELECT * FROM departments WHERE department_id = 280 ;

DELETE FROM departments WHERE department_id = 280 ;

Solution 6: Writing Control Structures


@@@@@@@@@@@@@@@@@@

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.

drop table messages ;

create table messages


(
results number
);

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.

a. In the declarative section of the block, declare a variable v_empno of type


emp.employee_id and initialize it to 176. Declare a variable v_asterisk of type
emp.stars and initialize it to NULL. Create a variable v_sal of type emp.salary

Ans:
====

create table emp


as
select *
from employees ;

alter table emp


add stars varchar2(50) ;

select employee_id, salary, STARS


from emp ;

select salary, NVL(ROUND(salary/1000), 0), stars


from emp
where employee_id = 176 ;

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;
/

select salary, NVL(ROUND(salary/1000), 0) rs, stars


from emp
where employee_id = 176 ;

SALARY RS STARS
---------- ---------- --------------------------
8600 9 *********

==========================

Page 4 of 4
Youtube Channel: https://www.youtube.com/@muhammadnurealam

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy