if for语句中的else语句

时间:2012-09-17 11:16:52

标签: database oracle plsql oracle10g

是否可以使用for循环

的if else条件

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;

当我尝试上述操作时,我遇到了编译错误。

此致

2 个答案:

答案 0 :(得分:3)

结构如下

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;

答案 1 :(得分:1)

使用for循环是不可能的,但如果你在循环中的动作在两种情况下相似,我会用游标来做。

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;