Assignment 5 - PLSQL Variables and Data Types
Assignment 5 - PLSQL Variables and Data Types
1. (12 pts) Enter the data type category and the data type for each value. The first one has been done
for you.
Index Name
1 ‘Mark’ Composite Table
2 ‘Katie’
2. (8 pts) Identify valid and invalid variable declaration from 1 to 4 and initialization.
For the invalid declarations above, describe why they are invalid.
The first one has been done for you:
SQLX DECLARE
today DATES := SYSDATE ;
tomorrow today%TYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE (tomorrow);
END;
4. (10 PTS) Write an anonymous block that uses a country name like ‘United States of America’ as input
and prints the highest and lowest elevations for that country. Use the COUNTRIES table. Execute the
query. Hint: Declare three variables and use them on SELECT clause.
DECLARE
v_country_name varchar2(50):= ‘United States of America’;
v_lowest_elevation number(6);
v_highest_elevation number(6);
BEGIN
SELECT lowest_elevation, highest_elevation
INTO v_lowest_election, v_highest_elevation
FROM COUNTRIES
WHERE country_name = v_country_name;
DBMS_OUTPUT.PUT_LINE(‘The lowest elecation for ‘||v_country_name ||’ is: ‘||
v_lowest_elecation);
DBMS_OUTPUT.PUT_LINE(‘The highest elevation for ‘||v_country_name ||’ is: ‘||
v_highest_elevation);
END
Write SQL code and take a result screenshot and paste it here.
Sample Result:
The lowest elevation for United States of America is -86
The higest elevation for United States of America is 6194
5. (10 pts) Evaluate the PL/SQL block below and determine the value of each of the following variables
according to the rules of scoping.
DECLARE
weight NUMBER(3) := 600;
message VARCHAR2(255) := 'Product 10012';
BEGIN
DECLARE
weight NUMBER(3) := 1;
message VARCHAR2(255) := 'Product 11001';
new_locn VARCHAR2(50) := 'Europe';
BEGIN
weight := weight + 1;
new_locn := 'Western ' || new_locn;
-- Position 1 --
END;
weight := weight + 1;
message := message || ' is in stock';
-- Position 2 --
END;
6. (5 pts) Enter and run the following PL/SQL block, which contains a nested block. Look at the output
and answer the questions.
DECLARE
v_employee_id employees.employee_id%TYPE;
v_job employees.job_id%TYPE;
BEGIN
SELECT employee_id, job_id INTO v_employee_id, v_job
FROM employees
WHERE employee_id = 100;
DECLARE
v_employee_id employees.employee_id%TYPE;
v_job employees.job_id%TYPE;
BEGIN
SELECT employee_id, job_id INTO v_employee_id, v_job
FROM employees
WHERE employee_id = 103;
DBMS_OUTPUT.PUT_LINE(v_employee_id || ' is a(n) ' || v_job);
END;
DBMS_OUTPUT.PUT_LINE(v_employee_id || ' is a(n) ' || v_job);
END;
Rewrite the code to display the details of employee 100 in the inner block. Use block labels.
Write SQL code and take a result screenshot of the result and paste it here.
DECLARE
v_employee_id employees.employee_id%TYPE;
v_job1 employees.job_id%TYPE;
BEGIN
v_job2 employees.job_id%TYPE;
BEGIN
SELECT employee_id, job_id INTO v_employee_id, v_job FROM employees WHERE employee_id
= 103;
END;
END;
What to submit
Submit your solution word file in E-Portfolio/File Exchange.