简体   繁体   中英

How to 'select into' / define a variable used by a cursor before a cursor

Trying to define an id so that the cursor doesn't have to look up the ID via a where clause each time the cursor is run. Is there a way I can do this? I've tried compiling the code with no success :( Below is my code: (Thanks in advance)

create or replace PROCEDURE PROC
is
CURSOR Sample IS
    SELECT distinct lpad(DATA_DATE, 7, 0) as DATA_DAY
    from (
  select  
      v.DATA_DATE,
      v.HR,
      sv.submission_value_id
   from value v    
   inner join submission s on s.value_id = v.value_id
   where sv.SUBMISSIONE_ID NOT IN (
      SELECT SUBMISSION_ID FROM FLAG WHERE FLAG.FLAG_ID = (SELECT FLAG_ID from FLAG where FLAG_TX = 'Processed / Calculated'))
     and v.data_date is not null
   )
   where data_date is not null;
 l_day days%ROWTYPE;

BEGIN
  SELECT FLAG_ID into v_flag_id from FLAG where FLAG_TX = 'Processed / Calculated';

What I am trying to do is save the cursor from having to look up that flag_id all the time, and instead have that already defined so the cursor can pull it. Is it possible to do that?

If I understood what you're asking (more by reading the title than the example you provided), I'd say that you need a parametrized cursor . Here's an example based on Scott's schema:

  • line 2: declaring a variable which gets its value in line 10
  • line 4: declaring a parametrized cursor (par_deptno is the parameter )
  • line 11: opening a cursor & passing a variable
  • the rest is self-understandable

SQL> set serveroutput on;
SQL> declare
  2    l_deptno dept.deptno%type;
  3
  4    cursor c_emp (par_deptno in dept.deptno%type) is
  5      select ename
  6      from emp
  7      where deptno = par_deptno;
  8    c_emp_r c_emp%rowtype;
  9  begin
 10    l_deptno := 10;
 11    open c_emp(l_deptno);
 12    loop
 13      fetch c_emp into c_emp_r;
 14      exit when c_emp%notfound;
 15      dbms_output.put_line(c_emp_r.ename);
 16    end loop;
 17    close c_emp;
 18  end;
 19  /
CLARK
KING
MILLER

PL/SQL procedure successfully completed.

SQL>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM