0

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

2 Answers 2

1

Since the assignment calls for a function, presumably it expects this to return a ref cursor, in which case it could be along the lines of (untested):

create or replace function newname
    ( lastname_in in employees.name%type )
    return sys_refcursor
as
    c_results sys_refcursor;
begin
    open c_results for
        select first_name
             , last_name
             , case last_name
                   when 'KING' then 'LION'
                   when 'FORD' then 'CAR'
                   when 'MILLER' then 'BEER'
                   else last_name
               end as new_name
        from   employees
        where  last_name = lastname_in
        order by last_name, first_name;

    return c_results;
end newname;

If a procedure is acceptable and "output" can mean dbms_output (normally a diagnostic/debugging tool and not suitable for production reporting), you might try something like this (untested):

create or replace procedure newname
    ( lastname_in in employees.name%type )
as
begin
    for r in (
        select last_name
             , case last_name
                   when 'KING' then 'LION'
                   when 'FORD' then 'CAR'
                   when 'MILLER' then 'BEER'
                   else last_name
               end as new_name
        from   employees
        where  last_name = lastname_in
        order by last_name, first_name
    )
    loop
        dbms_output.put_line(r.new_name);
    end loop;

end newname;

I don't know what course you are taking, but I do wonder why all beginners seem to go for the most convoluted cursor constructions possible (declare cursor, open, loop, fetch, exit-when etc). The standard for r in (...) loop construction is all you need in most cases (that's if you even need a loop at all) and is generally faster.

Also it's worth getting into the habit of laying out your code obsessively, with consistent use of upper/lower case and a standard indent (I've used 4 spaces, and I never use uppercase).

Sign up to request clarification or add additional context in comments.

Comments

0

Try this. Also there was no need for a cursor here as well. Since you are passing last_name, the function is anyways going to return a single value.

CREATE OR REPLACE FUNCTION changeName (lastname_in IN VARCHAR2)
   RETURN varchar2
IS

  CURSOR c_emp_record IS
  SELECT emp_name 
  FROM employee
  where emp_name = lastname_in ;

  v_emp_record employee.emp_name%TYPE;

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 v_emp_record LIKE '%KING%' THEN
         v_emp_record:='LION'; 
        DBMS_OUTPUT.PUT_LINE( v_emp_record)  ;

      Elsif  v_emp_record LIKE '%FORD%' THEN
         v_emp_record := 'CAR'; 
        DBMS_OUTPUT.PUT_LINE( v_emp_record)  ;

      ELSIF v_emp_record LIKE '%MILLER%' THEN
       v_emp_record := 'BEER'; 
        DBMS_OUTPUT.PUT_LINE( v_emp_record)  ;       

      END IF;
  END LOOP;

 Close c_emp_record ;
 return (v_emp_record);

End changeName;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.