2016-07-14 89 views
0

我使用的蟾蜍,有一個名爲MyTable表一個結果,它有一個名爲INFO柱:
信息
ABCD
EFGH
IJKL
蟾蜍:如何循環的選擇

我需要的是將INFO的元素一個接一個地完成任務。所以,我認爲我需要的東西,象下面這樣:

foreach (select INFO from MyTable) 
    print 
end 

我試圖谷歌,似乎我應該使用光標。所以,我想是這樣的:

DEF msg varchar2(15); 

cursor cr is 
    select info from mytable; 

begin 
    OPEN cr; 
    loop 
    FETCH cr into msg; 
    exit when cr%NOTFOUND; 
    -- do job 
    end loop; 
    CLOSE cr; 
end; 

但我得到了一個錯誤:

cursor cr is
Error at line 3
ORA-00900: invalid SQL statement
Script Terminated on line 3.

回答

1

顯然要執行PL/SQL塊,但DEF不是PL/SQL的一部分。 嘗試執行下列程序:

declare 
msg varchar2(15); 
cursor cr is 
    select info from mytable; 
begin 
    OPEN cr; 
    loop 
    FETCH cr into msg; 
    exit when cr%NOTFOUND; 
    -- do job 
    end loop; 
    CLOSE cr; 
end; 

你也可以做同樣的使用cursor for loop statement

begin 
    for rec in (
    select info from mytable 
) loop 
     -- do job (you can reference info by using rec.info) 
    end loop; 
end;