Lab 9 Sp18 208A
Lab 9 Sp18 208A
Lab 9 Sp18 208A
A. Test your function from an anonymous block which uses a local variable to store
and display the returned value.
B. Modify your anonymous block from the previous step to remove the local
variable declaration and call the function directly from within the
DBMS_OUTPUT.PUT_LINE call. Test the block again.
C. Now call the function from within a SELECT statement, not a PL/SQL block. Your
SELECT statement should display the first_name, last_name, and full name (using
the function) of all employees in department 50. Your output should look like this.
Lab_9_Sp18_208A.doc
3.. Create a function called divide that accepts two numbers as input and returns the
result of dividing the first number by the second number, rounded to two decimal
places. Save your code.
A. Test your function twice from an anonymous block using input values (50,2)
and (25,3).
B. Test your function a third time using input values (16,0). What happens?
C. Modify the function code to trap the ZERO_DIVIDE exception. The exception
handler should return a value of zero from the function if ZERO_DIVIDE is
raised.
D. Test your function again using input values (16,0) as before. Now what happens?
The questions in this Practice use partial copies of the employees and departments tables.
Create these copies by executing the following SQL statements:
Lab_9_Sp18_208A.doc
1. Create and execute a function sal_increase using the following two code samples.
The first creates a function that returns an employee’s new salary if a percentage
increase is granted. The second calls this function in a SELECT statement, using an
increase of 5 percent.
Now, suppose you want to see the same information in your SELECT statement, but
only for those employees for whom the increased salary would be greater than
10000. Write and test two SELECT statements to do this. In the first, do NOT use
your function. In the second, use your function. Use an increase of 5 percent. Which
do you think is better, and why?
2. Name five places within a SQL statement where a function can be used. The first one
has been done for you (think of four more).
3. Modify your anonymous block from question 1 (the block with the calls to the
sal_increase function) to ORDER the results by the increased salary in descending order
(i.e., highest increased salary first)
Lab_9_Sp18_208A.doc
4. Examine the following SELECT statement that lists the total salaries in each
department, for those departments whose total salary is greater than 20000.
Modify the statement so that it also lists the total salary in each department if a
5 percent increase is granted, and lists those departments whose increased
total salary would be greater than 20000. Your modified statement should call
the sal_increase function twice, once in the column_list and once in the
HAVING clause. Test the modified statement.
A) The Data Dictionary is a list of hard coded table names in all Oracle databases.
B) The Data Dictionary can be updated by all users with SELECT statements.
C) All users of an Oracle Database can see details of all tables in that database.
D) The Data Dictionary is owned by the user called SYS.
3. Write and execute a SELECT statement that lists all the stored objects you have
created in your account so far. The query should return the object name and type and its
status. Order the output by type of object.
Lab_9_Sp18_208A.doc
Section 9-4, Exercise #1 A-E, 2, 3, 4
B. Examine the following code. Create the procedure. Save your work (you will
need to modify the procedure code later).
C. What do you think would happen if you execute this procedure to insert
department_id 10 (which already exists)? Write and execute an anonymous block
to test your theory.
E. Now what do you think would happen if you execute this procedure for
department_id 10 (which already exists)? Test it again as in step c.
Lab_9_Sp18_208A.doc
F. Modify the procedure code to leave out the exception section again. Run the code.
G. Execute the following code to create a new procedure called outer_proc which calls
add_my_dept, passing department_id 10 to it:
2. Write and execute a SELECT statement to list the names of all the procedures you
have created so far.
4. Write and execute a SELECT statement to list the source code of your add_my_dept
procedure. Make sure your SELECT statement list the lines of code in the correct order.
Lab_9_Sp18_208A.doc
Section 9-5, Exercise #1, 2
1. If you wanted user SUSAN to be able to execute SELECT and all DML statements on
your countries table, what SQL statement would you execute to give her the required
privileges?
2. User TOM creates a table called TOMTAB, and does not grant you any privileges on
it.
B. Examine the following code. Now the INSERT statement has been included in a
procedure which you have created. Will it work now?
Lab_9_Sp18_208A.doc
D. TOM now REVOKEs your INSERT privilege on tomtab. TOM then writes the
following procedure. Which privilege must TOM grant to you to allow you to
execute his tom_ins_proc procedure? With this privilege, will the INSERT work
when you invoke TOM’s procedure?
The following questions illustrate how definer’s and Invoker’s rights work.
1. Assume the following two procedures have been created in an account called
IACAD_SCHEMA, which also contains an EMPS table:
Lab_9_Sp18_208A.doc
CREATE OR REPLACE PROCEDURE show_emps_inv (p_emp_id IN NUMBER)
AUTHID CURRENT_USER
IS
v_name emps.name%TYPE;
v_dept_id emps.department_id%TYPE;
v_dept_name emps.department_name%TYPE;
v_sal emps.salary%TYPE;
BEGIN
SELECT name, department_id, department_name, salary
INTO v_name, v_dept_id, v_dept_name, v_sal
FROM emps
WHERE employee_id = p_emp_id;
DBMS_OUTPUT.PUT_LINE('The employee details are: '|| v_name
||' '|| v_dept_id
||' '|| v_dept_name
||' '|| v_sal);
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('No data found for employee id : '
|| p_emp_id||
'. Sorry, please enter another number and try again.');
END;
A. DESCRIBE both procedures, to verify that you can see them in your account.
Remember to prefix the procedure name with the schema name.
B.
Execute a SQL statement to try to select directly from the table used in the
procedures.
2. Execute the first procedure (show_emps_def) with the following actual parameter
value: employee_id = 100. What happens and why?
3.. Execute the first procedure again, this time with employee_id = 10. What happens and
why?
Lab_9_Sp18_208A.doc
4. Execute the second procedure (show_emps_inv) with employee_id = 100. What happens and
why?
Lab_9_Sp18_208A.doc