2012-09-17 306 views
0

如果else條件有for循環,是否可能有條件if for else語句在for循環中

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; 

當我嘗試上述我有編譯錯誤。

問候

回答

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; 
+0

感謝您指出這一點。問候 – user75ponic

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;