1

I am new to this group. I was trying to think of having a cursor with condition select statement. Some how like this pseudo code--

[B]cursor test_cursor is
  if condition == 't11'
  then 
    select * from test1;
  else 
    select * from test1;
  end if;[/B]
begin

  for cursorVal in test_cursor loop
    //Doing the actual task on cursor data.
  end loop;

commit;
end;

Actually, i came across with a scenario where need to work on two different tables with same DDL. Based on some user input, need to fetch data from either of the table and further manipulate in procedure. As i said both table are of same DDL so don't want to create two different cursor. The reason for this same business logic will be applied on both tables data. Its just the user input which decide which table need to fetch data. Some how one can think of this as latest data and historical data and the way DB is designed.

Hope i am clear with my scenario.

Thanks, Arfeen.

4
  • 1
    So what is your question? Commented Jan 24, 2014 at 7:23
  • Use dynamic SQL. See docs.oracle.com/cd/E11882_01/appdev.112/e25519/… Commented Jan 24, 2014 at 7:23
  • 1
    Using ref cursor can solve your problem, based on user inputs you can open that cursor for desired table. Commented Jan 24, 2014 at 7:28
  • Thank you all for your help. I resolve this issue with the hint given by San. Commented Jan 30, 2014 at 20:23

3 Answers 3

1

The cursor can be declared as a union as described below. Depending on the content of variable condition, the cursor will either be based on Test1 or Test2.

SELECT * FROM Test1 WHERE condition = 't1'
UNION ALL
SELECT * FROM Test2 WHERE condition = 't2'
Sign up to request clarification or add additional context in comments.

Comments

0

What you are trying to achieve looks like it could either be achieved by better table or view design or by using a BULK COLLECT. If you can - always consider database design first over code.

BEGIN

if condition == 't11' then
 SELECT XXXXXX
 BULK COLLECT INTO bulk_collect_ids
 FROM your_table1;
else
 SELECT XXXXXX
 BULK COLLECT INTO bulk_collect_ids
 FROM your_table2;
end if;

FOR indx IN 1 .. bulk_collect_ids.COUNT
LOOP
 .
 //Doing the actual task on bulk_collect_ids data.
 .
END LOOP;

END;

Comments

0

I usually prefer to do the below if I have to conditionally open a cursor.

The condition can be passed as a function/procedure input or a simple variable.

declare 
  vSwitch varchar2(10):='FIRST';
  vRefCursor SYS_REFCURSOR;
  vLimit     NUMBER := 100;
  
  Type t_tbl is table of my_table%rowtype index by binary_integer;
  vtbl  t_tbl;

begin

  if vSwitch = 'FIRST' then 
   open vRefCursor FOR 
        select /*+ parallel (4) */
             column1,column2,column3
         from my_table 
           where
           some_complex_check;
  else
    open vRefCursor FOR 
        select /*+ parallel (4) */
             column1,column2,column3
         from my_table 
           where
           some_other_complex_check;
  end if;

 loop
      fetch vRefCursor bulk collect into vtbl limit vLimit;
       
      exit when vtbl.count=0;
      begin 
         forall idx in vtbl.first..vtbl.last
         insert /*+ enable_parallel_dml parallel(4)*/
            my_table(column1,column2,column3)
          values 
           (vtbl(idx).column1,vtbl(idx).column2,vtbl(idx).column3);
      commit;
      exception when others then 
       ---    handle DML exceptions
      end;


 end loop;

end;

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.