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

user565992
  • 497
  • 2
  • 10
  • 17
  • seems to be already answered [link](http://stackoverflow.com/questions/4864404/conditionally-define-a-cursor-in-oracle) – Sudipta Mondal Apr 12 '17 at 14:33
  • Possible duplicate of [Conditionally define a Cursor in Oracle](http://stackoverflow.com/questions/4864404/conditionally-define-a-cursor-in-oracle) – Sudipta Mondal Apr 12 '17 at 14:34

1 Answers1

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;
/
Graham
  • 7,431
  • 18
  • 59
  • 84
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • Also, I think, the OP might have a table say `order` which will store the variable `status = complete`. – Sudipta Mondal Apr 12 '17 at 14:39
  • 1
    @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... – Alex Poole Apr 12 '17 at 14:45
  • @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.) – Alex Poole Apr 12 '17 at 17:20
  • @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. – J. Chomel Apr 13 '17 at 06:04
  • @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. – J. Chomel Apr 13 '17 at 06:05
  • WHY? because you can re-use it multiple times if you need and your code looks better then 100 lines of select statement – TheName Apr 13 '17 at 07:22
  • @TheName, I don't think Alex will disagree there shouldn't be 300 lines inside the loop - but that's not the most important here. – J. Chomel Apr 13 '17 at 07:28
  • @thename, now I get it what you meant. It's useful to declare cursors in case we need you reuse them. Rarely the case in my field. – J. Chomel Apr 15 '17 at 06:20
  • @J.Chomel I'ts the worst practise to use sql in pl/sql, because those things is the case of errors. – TheName Apr 15 '17 at 11:31
  • @TheName, I guess I understand why now... Good naming convention should save us :) – J. Chomel Apr 15 '17 at 16:44
  • @user565992, did it help you achieve what you intended? I didn't understand why I got downvoted here. – J. Chomel Apr 19 '17 at 11:45