I am trying to do the following by using a cursor to change by employee last name
Create a PLSQL function, which when used on a SQL statement would output the last names of all the employees in the employees table according to the following logic:
- If the name of the employee is KING, the function should output it as LION.
- If the name of the employee is FORD, the function should output it as CAR
- If the name of the employee is MILLER, then the function should change it to BEER.
Otherwise, the name should be outputted as it is listed in the employees table.
So far I have have this:
set serveroutput on;
CREATE OR REPLACE FUNCTION changeName (lastname_in IN VARHCAR2 )
RETURN c_emp_record
IS
declare
CURSOR c_emp_record IS
SELECT last_name FROM employees;
v_emp_record c_emp_record%ROWTYPE;
begin
OPEN c_emp_record;
DBMS_OUTPUT.PUT_LINE('LastName'||' ' );
LOOP
FETCH c_emp_record into v_emp_record;
EXIT WHEN c_emp_record%NOTFOUND;
if c_emp_record IN ('%KING%') THEN
DBMS_OUTPUT.PUT_LINE(' LION' );
elsif c_emp_record LIKE '%FORD%' THEN
DBMS_OUTPUT.PUT_LINE(' CAR' );
ELSIF c_emp_record LIKE '%MILLER%' THEN
DBMS_OUTPUT.PUT_LINE('BEER' );
ELSE
DBMS_OUTPUT.PUT_LINE(v_emp_record.last_name || 'No change');
END IF;
END LOOP;
end changeName;
I am not sure if I am using the cursor for this problem correctly