ref游标是否可以像for游标一样用于for循环?即喜欢
for i in cur_name
loop
dbms_output.put_line(i.column_name)
end loop;
如果不可能那么为什么?
答案 0 :(得分:3)
您不能像对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'不是程序或未定义
ref cursor
是指向open cursor
的指针,用于将打开的游标作为out参数发送到客户端应用程序以循环记录。
如果你想循环,那么
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;
/