UBD08
UBD08
UBD08
8
Observație!
Scrieți rezolvarea direct în acest document!
Creating Functions
1. Function full_name:
A. Create a function called full_name. Pass two parameters to the function: an employee’s last name
and first name. The function should return the full name in the format: last name, comma and space,
first name (for example: Smith, Joe). Save your code.
B. Test your function from an anonymous block which uses a local variable to store and display the
returned value.
DECLARE
v_full_name VARCHAR2(50);
BEGIN
v_full_name := full_name('Smith', 'Joe');
DBMS_OUTPUT.PUT_LINE('The full name is ' || v_full_name);
END;
C. Modify your anonymous block from step b to remove the local variable declaration and call the
function directly from within the DBMS_OUTPUT.PUT_LINE call. Test the block again.
BEGIN
DBMS_OUTPUT.PUT_LINE('The full name is ' || full_name('Smith', 'Joe'));
END;
D. Now call the function from within a SQL SELECT statement. Execute a SQL statement (not a
PL/SQL block) which displays the first_name, last_name and full name (using the function) of all
employees in department 50.
2. Function reverse_string:
A. Create a function which accepts a character string as input and returns the same character string
but with the order of the letters reversed. For example ‘Smith’ would be returned as ‘htimS’. Save
your code. Hint: you will need to declare a local variable to store the reversed string, and build its
contents by reading the input one character at a time (using SUBSTR) in a loop structure, starting
from the last character. Each execution of the loop reads the preceding character and concatenates it
to the reversed string.
A. Create and execute a function sal_increase using the following two code samples. The first creates
a function which 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.
B. 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.
1. 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.
2. Change the query from question 3 to show all functions and procedures to which you have
access. Include the owner of the object as well.
1. If you wanted user SUSAN to be able to execute SELECT and all DML statements on your
wf_countries table, what SQL statement would you execute to give her the required privileges?
2. Another user TOM creates a table called tomtab, and does not grant you any privileges on it.
No, because if I don’t have any privileges granted, it means I can’t insert rows into the table.
B. Examine the following code. Now the INSERT statement has been included in a procedure which
you have created. Will it work now?
Even though I try to insert rows through a procedure, because I don’t have any privileges the insert
won’t work and I also need the EXECUTE privilege.
Tom needs to give me the EXECUTE privilege so I can execute the procedure. In this case the
INSERT will work.