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

Assignment 5 - PLSQL Variables and Data Types

The document contains instructions for an assignment on PL/SQL variables and data types. It provides examples of declaring different types of variables like strings, numbers, dates and assigning values to them. It also includes examples of using variables in anonymous blocks to retrieve data from tables and display output. Students are asked to complete tasks like identifying valid variable declarations, writing code to query a database table and display the results.

Uploaded by

Meyers323
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Assignment 5 - PLSQL Variables and Data Types

The document contains instructions for an assignment on PL/SQL variables and data types. It provides examples of declaring different types of variables like strings, numbers, dates and assigning values to them. It also includes examples of using variables in anonymous blocks to retrieve data from tables and display output. Students are asked to complete tasks like identifying valid variable declarations, writing code to query a database table and display the results.

Uploaded by

Meyers323
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Your Name: Michael Meyers

Assignment 5: PL/SQL Variables and Data Types


Please write your name at the top of the sheet and use a different color than black when writing
answers.

1. (12 pts) Enter the data type category and the data type for each value. The first one has been done
for you.

Value Data Type category Dat Type


‘James’ Scalar VARCHAR2
Text of resume Scalar VARCHAR2
100.20 Scalar VARCHAR2
Picture Lob Blob
FALSE Scalar Number
11-Feb-2021 Scalar Date

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:

Declaration Valid or invalid


studentID INTEGER; Valid
studentName VARCHAR2(10) := Johnson Invalid
String literals
should be
enclosed within
single
quotation
marks and
because := is
used to assign
values
stu_per_class CONSTANT NUMBER Invalid
Constant
variable must
be initialized
during
declarartion
tomorrow DATE := SYSDATE + 1 Valid
flag BOOLEAN NOT NULL := TRUE; Invalid
Cannot
determine if a
null value is
TRUE or FALSE

3. (5 pts) Write an anonymous block that declares the following variables:


• A variable named today of datatype DATE. Initialize today with SYSDATE.
• A variable named tomorrow with the same datatype as today . Use the %TYPE attribute to
declare the variable, tomorrow.

SQLX DECLARE
today DATES := SYSDATE ;
tomorrow today%TYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE (tomorrow);
END;

Then, display the value of tomorrow onto the screen.


Write SQL code and take a result screenshot and paste it here.

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;

a) The value of weight at position 1 is: 2


b) The value of new_locn at position 1 is: Western Europe
c) The value of weight at position 2 is: 3
d) The value of message at position 2 is: Product 11001 is in stock
e) The value of new_locn at position 2 is: Western Europe

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

SELECT employee_id, job_id INTO v_employee_id, v_job1 FROM employees WHERE


employee_id = 100;

DECLARE v_employee_id employees.employee_id%TYPE;

v_job2 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_job1);

END;

DBMS_OUTPUT.PUT_LINE(v_employee_id || ' is a(n) ' || v_job1);

END;

What to submit
Submit your solution word file in E-Portfolio/File Exchange.

You might also like