Search code examples
oracle-databaseplsqloracle12c

Select from explicit cursor


Is there a way to select from an explicit cursor without having to use a loop with IF conditions to filter the cursor?

In other words, if I have the following cursor...

CURSOR cur_emp IS
  SELECT * FROM emp;

is there a way to do the following?

DECLARE
  v_row cur_emp%ROWTYPE;
BEGIN
  SELECT * INTO v_row FROM cur_emp WHERE id_emp < 10;
  /* Do something */
END;

Solution

  • No but change the cursor instead

    CURSOR cur_emp(iEmpid in emp.id_emp%TYPE) IS
      SELECT * FROM emp
       WHERE id_emp < iEmpid;
    

    and

    for rEmp in cur_emp(10) loop
      -- do something
    end loop;
    

    OR

    Skip the cursor and put the hole thing into the for loop, its just as effective.

    for rEmp in (SELECT * FROM emp WHERE id_emp < 10) loop
      -- do something
    end loop;