0

Could it be possible to have if else condition with a for loop

E.g.

  IF (emp_no IS NULL) then
 for i in (select * from employees where emp_no= p_retval)
else
 for i in (select * from employees where emp_no= p_retval_withcond)
end if;

When I tried the above I have got compilation errors.

Regards

2 Answers 2

3

The structure is as follows

IF (emp_no IS NULL) then
  for i in (select * from employees where emp_no= p_retval)
  loop
    -- loop actions
  end loop;
else
  for i in (select * from employees where emp_no= p_retval_withcond)
  loop
    -- second loop actions
  end loop;
end if;
Sign up to request clarification or add additional context in comments.

Comments

1

it is not possible with a for loop but if your actions in the loop are similar in the two cases I would do this by cursors.

declare
cursor c1 is select * from employees where emp_no= p_retval;
cursor c2 is select * from employees where emp_no= p_retval_withcond;
ligne employees%rowtype;
.....
begin
   IF (emp_no IS NULL) then
      open c1;
   ELSE
      open C2;
   END IF;
   loop
      IF (emp_no IS NULL) then
         fetch C1 into ligne;
         exit when c1%notfound;
      ELSE
         fetch C2 into ligne;
         exit when c2%notfound;
      END IF;
      -- loop actions
      ....  
   end loop;
   IF (emp_no IS NULL) then
      close c1;
   ELSE
      close C2;
   END IF;
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.