3

ref 游标可以像游标一样用于 for 循环吗?即喜欢

for i in cur_name 
loop
dbms_output.put_line(i.column_name)
end loop; 

如果不可能,那为什么?

4

1 回答 1

6

你不能for像对一个implicit/explicit cursors

declare
    ref_cur sys_refcursor;
BEGIN
    OPEN ref_cur FOR SELECT table_name FROM all_tables WHERE ROWNUM < 5;
    for i in ref_cur loop
        dbms_output.put_line(i.table_name); 
    end loop;  
END;
/

你会得到一个错误

PLS-00221:“REF_CUR”不是过程或未定义

Aref cursor是指向open cursor用于将打开的游标作为输出参数发送到客户端应用程序以遍历记录的指针。
如果你想循环过去,

declare
    ref_cur sys_refcursor;
    v_name all_tables.table_name%TYPE;
BEGIN
    OPEN ref_cur FOR SELECT table_name FROM all_tables WHERE ROWNUM < 5;
    LOOP
        FETCH  ref_cur INTO  v_name;
        exit when ref_cur%notfound;
        dbms_output.put_line(v_name); 
    end loop;  
END;
/

如果implicit/explicit cursors光标的生命周期在*block*

declare
    cursor cur IS SELECT table_name FROM all_tables WHERE ROWNUM < 5;
BEGIN 
    for i in cur loop
        dbms_output.put_line(i.table_name); 
    end loop;
END;
/

或者

BEGIN 
    for i in (SELECT table_name FROM all_tables WHERE ROWNUM < 5) loop
        dbms_output.put_line(i.table_name); 
    end loop;
END;
/
于 2015-12-15T13:11:42.770 回答