1

I have two cursors the for loop should use the cursor based on the status.

CURSOR order_hist1 IS 
   SELECT id, ordernum, address FROM order_hist; 

CURSOR order_hist2 IS 
   SELECT id, ordernum, address FROM order_hist_complete; 

so for loop should use cursor order_hist2 is the variable status = 'COMPLETE' else use order_hist1

 FOR aDistinctLine in --     LOOP
   -- 300 lines code in this loop
 END LOOP;

I don't want o use REF Cursors

2

1 Answer 1

1

You can use implicit for loop:

  • For your case, it looks suitable to change the two cursors to a single one, using UNION (UNION ALL if you need to process duplicates, or performance reasons), like follows:

FOR aDistinctLine in (
  -- first cursor: status <> COMPLETE
  SELECT id, ordernum, address FROM order_hist 
   WHERE status <> 'COMPLETE' 
  UNION 
  SELECT id, ordernum, address FROM order_hist_complete 
   WHERE status = 'COMPLETE' 
) LOOP

-- do things with 
--     aDistinctLine.id, 
--     aDistinctLine.ordernum, 
--     aDistinctLine.address 

END LOOP;

Then it's better to have status look like a local variable, e.g. call it l_status; I had to convince myself it could work to use a plsql variable inside an implicit for loop... guess I learned something today!

declare
  l_status varchar2(8) := 'COMPLETE';
begin
  for x in (select 'realy?' val from dual where l_status = 'COMPLETE')
  loop
    dbms_output.put_line(x.val);
  end loop;

  l_status := 'graby';
  for x in (select 'here: not complete' val from dual where l_status <> 'COMPLETE')
  loop
    dbms_output.put_line(x.val);
  end loop;
end;
/
Sign up to request clarification or add additional context in comments.

11 Comments

Also, I think, the OP might have a table say order which will store the variable status = complete.
@SudiptaMondal - the question says status is a variable (though this does assume the table sin the cursor don't also have a column with that name; you can only go on what the OP tells you though). And what 2000 lines? You can still use a single explicit cursor this way if you prefer - you can even pass the status variable as as a formal argument...
@J.Chomel - I'm confused; why is the second option with the procedure necessary? The OP's original 300 lines can just go inside the single loop in your first option, surely, ...status being a variable rather than a column name doesn't make any difference, does it? (A naming convention that makes it clear it's a variable would be nice, but if there's no scope clash that's kind of cosmetic.)
@AlexPoole, eidited; I guess I'm the most confused... I didn't know it could work this way. So I was right for being twice wrong. Sp0ooky.
@user565992, status doesn't need to be part of the tables! See my edited answer with a snipet to test it works with a local variable.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.