【问题标题】:If else statement in for loopfor循环中的if else语句
【发布时间】:2012-09-17 11:16:51
【问题描述】:

是否有可能在 for 循环中设置 if else 条件

例如

  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;

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

问候

【问题讨论】:

    标签: database oracle plsql oracle10g


    【解决方案1】:

    结构如下

    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;
    

    【讨论】:

      【解决方案2】:

      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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-28
        • 1970-01-01
        相关资源
        最近更新 更多