Lab 6 Adms
Lab 6 Adms
Class: BSCS-13
Objectives
The objective of this Lab is to understand the basics of Conditional statements in PL/SQL.
Tools/Software Requirement
SQL Developer
VSCode
or you can directly run your commands in
https://livesql.oracle.com/next/
Description
PL/SQL includes various conditional statements that allow developers to execute different blocks
of code based on specific conditions. Decision-making statements in programming
languages decide the direction of the flow of program execution. Conditional Statements available
in PL/SQL are defined below:
1. IF THEN
2. IF THEN ELSE
3. NESTED-IF-THEN
4. IF THEN ELSIF-THEN-ELSE Ladder
IF THEN
if then the statement is the most simple decision-making statement. It is used to decide whether a
certain statement or block of statements will be executed or not i.e if a certain condition is true
then a block of statement is executed otherwise not.
Example:
-- pl/sql program to illustrate If statement
declare
num1 number:= 10;
num2 number:= 20;
begin
end;
IF THEN ELSE
The if statement alone tells us that if a condition is true it will execute a block of statements and if
the condition is false it won’t. But what if we want to do something else if the condition is false.
Here comes the else statement. We can use the else statement with if statement to execute a block
of code when the condition is false.
Example
-- pl/sql program to illustrate If else statement
declare
num1 number:= 10;
num2 number:= 20;
begin
ELSE
dbms_output.put_line('i am in else Block');
end if;
NESTED-IF-THEN
A nested if-then is an if statement that is the target of another if statement. Nested if-then
statements mean an if statement inside another if statement. Yes, PL/SQL allows us to nest if
statements within if-then statements. i.e, we can place an if then statement inside another if then
statement.
Example
-- pl/sql program to illustrate nested If statement
declare
num1 number:= 10;
num2 number:= 20;
num3 number:= 20;
end if;
Lab Task
1) Write a PL/SQL program to find out how long an employee has been working at the
company.
• Check their hire date from the EMPLOYEES table.
• Compare it with today's date (Hint: SYSDATE).
• Based on their experience, display one of the following messages:
a. "New Employee" If they’ve been with the company for less than a year.
b. "Experienced Employee" If they’ve worked for 1 to 5 years.
c. "Senior Employee" If they’ve been there for more than 5 years.
CODE:
DECLARE
v_first_name HR.EMPLOYEES.FIRST_NAME%TYPE;
v_last_name HR.EMPLOYEES.LAST_NAME%TYPE;
v_employee_id HR.EMPLOYEES.EMPLOYEE_ID%TYPE;
v_hire_date HR.EMPLOYEES.HIRE_DATE%TYPE;
v_experience NUMBER;
BEGIN
v_employee_id := &Enter_Employee_ID;
--Calculating Per Year Experience ( TRUNC or truncate (remove the decimal part) of a number or date.)
v_experience := TRUNC ( MONTHS_BETWEEN ( SYSDATE , v_hire_date ) / 12 );
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with employee ID ' || v_employee_id );
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
OUTPUT:
2) In this task you have to categorize countries based on their GDP status using the
COUNTRIES table in the HR schema. The goal is to check the GDP category of a given
country and print the result like this.
a. US, DE, JP (Developed Country)
b. IN, CN, BR (Emerging Economy Others)
c. Others (Developing Countries)
CODE:
DECLARE
v_Country_Id HR.COUNTRIES.COUNTRY_ID%TYPE;
Country_Name HR.COUNTRIES.COUNTRY_NAME%TYPE;
BEGIN
v_Country_Id := TO_CHAR('&Enter_Country_Id');
SELECT COUNTRY_NAME
INTO Country_Name
FROM HR.COUNTRIES
WHERE COUNTRY_ID = v_Country_Id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No country found with the provided Country ID.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
OUTPUT:
CODE:
DECLARE
v_job_id HR.JOBS.JOB_ID%TYPE;
v_job_title HR.JOBS.JOB_TITLE%TYPE;
BEGIN
FROM HR.JOBS
ELSE
END IF;
END;
/
CS236: Advance Database Systems Page 7
OUTPUT:
Deliverables:
Submit a PDF document including the SQL queries to answer above-mentioned information needs
as well as snapshot of their outcome when executed over MySQL using the Workbench.