PLSQL Fundamentals Practice
PLSQL Fundamentals Practice
As you perform practices in the course, you will develop a simple application using an
anonymous block. This anonymous block covers the following:
• Writing a declarative section
• Declaring variables of scalar types
• Declaring variables using the %TYPE attribute
• Writing an executable section
• Accepting user inputs for variables
• Retrieving the values from the database and storing the values in the variables by using
the INTO clause
• Writing a nested block within the executable section
• Using the control structures in the executable section to perform business logic
• Using the INDEX BY table to store values and print them
• Handling exceptions
department_id department_name
20 Marketing
60 IT
80 Sales
100 Finance
110 Accounting
a. BEGIN
END;
b. DECLARE
amount INTEGER(10);
END;
c. DECLARE
BEGIN
END;
d. DECLARE
amount INTEGER(10);
BEGIN
DBMS_OUTPUT.PUT_LINE(amount);
END;
2. Create and execute a simple anonymous block that outputs “Hello World.” Execute
and save this script as lab_01_02_soln.sql.
3. Examine the following anonymous block and choose the appropriate statement.
SET SERVEROUTPUT ON
DECLARE
fname VARCHAR2(20);
lname VARCHAR2(15) DEFAULT 'fernandez';
BEGIN
DBMS_OUTPUT.PUT_LINE( FNAME ||' ' ||lname);
END;
/
a. The block will execute successfully and print ‘fernandez’
b. The block will give an error because the fname variable is used without
initializing.
c. The block will execute successfully and print ‘null fernandez’
d. The block will give an error because you cannot use the DEFAULT keyword to
initialize a variable of type VARCHAR2.
e. The block will give an error because the variable FNAME is not declared.
1. Evaluate the PL/SQL block given above and determine the data type and value of each
of the following variables according to the rules of scoping.
a. The value of weight at position 1 is:
2. In the PL/SQL block shown above, determine the values and data types for each of the
following cases.
a. The value of customer in the nested block is:
4. Accept a value at run time using the substitution variable. In this practice, you will
modify the script that you created in exercise 3 to accept user input.
a. Load the script lab_03_04.sql file.
b. Include the PROMPT command to prompt the user with the following message:
‘Please enter your employee number.’
c. Modify the declaration of the empno variable to accept the user input.
d. Modify the select statement to include the variable empno.
e. Execute and save your script as lab_03_04_soln.sql. Sample output is
shown below.
2. Modify the PL/SQL block you created in exercise 1 to insert a new department into the
departments table.
a. Load the script lab_04_01_soln.sql. Declare two variables:
dept_name of type department.department_name.
Bind variable dept_id of type NUMBER.
Assign ‘Education’ to dept_name in the declarative section.
b. You have already retrieved the current maximum department number from the
departments table. Add 10 to it and assign the result to dept_id.
c. Include an INSERT statement to insert data into the department_name,
department_id, and location_id columns of the departments table.
Use values in dept_name, dept_id for department_name,
department_id and use NULL for location_id.
d. Use the SQL attribute SQL%ROWCOUNT to display the number of rows that are
affected.
e. Execute a select statement to check if the new department is inserted. You can
terminate the PL/SQL block with “/” and include the SELECT statement in your
script.
f. Execute and save your script as lab_04_02_soln.sql. Sample output is
shown below.
2. Execute the script lab_05_02.sql. 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_05_02_soln.sql.
a. Use the DEFINE command to define a variable called empno and initialize it to
176.
b. Start the declarative section of the block and pass the value of empno to the
PL/SQL block through an iSQL*Plus substitution variable. Declare a variable
asterisk of type emp.stars and initialize it to NULL. Create a variable
sal of type emp.salary.
c. In the executable section write logic to append an asterisk (*) to the string for
every $1000 of the salary amount. For example, if the employee earns $8000, the
string of asterisks should contain eight asterisks. If the employee earns $12500,
the string of asterisks should contain 13 asterisks.
d. Update the stars column for the employee with the string of asterisks. Commit
before the end of the block.
e. You may want to execute and test the PL/SQL block for the countries with the
IDs DE, UK, US.
2. Create a PL/SQL block to retrieve the name of some departments from the
departments table and print each department name on the screen, incorporating an
INDEX BY table. Save the script as lab_06_02_soln.sql.
a. Declare an INDEX BY table dept_table_type of type
departments.department_name. Declare a variable my_dept_table
of type dept_table_type to temporarily store the name of the departments.
b. Declare two variables: loop_count and deptno of type NUMBER. Assign 10
to loop_count and 0 to deptno.
c. Using a loop, retrieve the name of 10 departments and store the names in the
INDEX BY table. Start with department_id 10. Increase deptno by 10 for
every iteration of the loop. The following table shows the department_id for
which you should retrieve the department_name and store in the INDEX BY
table.
DEPARTMENT_ID DEPARTMENT_NAME
10 Administration
20 Marketing
30 Purchasing
40 Human Resources
50 Shipping
60 IT
70 Public Relations
80 Sales
90 Executive
100 Finance
d. Using another loop, retrieve the department names from the INDEX BY table and
display them.
e. Execute and save your script as lab_06_02_soln.sql. The output is shown
below.
Department ID Message
10 Whalen Due for a raise
20 Hartstein Not Due for a raise
Fay Not Due for a raise
50 Weiss Not Due for a raise
Fripp Not Due for a raise
Kaufling Not Due for a raise
Vollman Not Due for a raise
Mourgas Not Due for a raise
. . .
. . .
Rajs Due for a raise
80 Russel Not Due for a raise
Partners Not Due for a raise
Errazuriz Not Due for a raise
Cambrault Not Due for a raise
. . .
. . .
For example, if the salary of the employee is less than 6500, then increase the
salary by 20 percent. In every WHEN clause, concatenate the first_name and
last_name of the employee and store it in the INDEX BY table. Increment the
value in variable i so that you can store the string in the next location. Include an
UPDATE statement with the WHERE CURRENT OF clause.
f. Close the loop. Use the %ROWCOUNT attribute and print the number of records
that were modified. Close the cursor.
g. Include a simple loop to print the names of all the employees whose salaries were
revised.
Note: You already have the names of these employees in the INDEX BY table.
Look for the comment “CLOSE THE INNER BLOCK” and include an END IF
statement and an END statement.
h. Save your script as lab_07_04_soln.sql.
2. The purpose of this example is to show how to declare exceptions with a standard
Oracle server error. Use the Oracle server error ORA-02292 (integrity constraint
violated – child record found).
a. In the declarative section declare an exception childrecord_exists.
Associate the declared exception with the standard Oracle server error –02292.
b. In the executable section display ‘Deleting department 40.....’. Include a
DELETE statement to delete the department with department_id 40.