oracle 游标怎么写,Oracle 游标的各种写法

oracle 游标怎么写,Oracle 游标的各种写法for..in`隐式游标`BEGINFORRSIN(SELECTcolumn_1FROMtable_name)LOOPdbms_output.put_line(RS.column_1);ENDLOOP;END;RS必须是单个字段DECLARECURSORRSISSELECTcolumn_1FROMtable_name;column_1VARCHAR…

大家好,欢迎来到IT知识分享网。oracle 游标怎么写,Oracle 游标的各种写法

— for .. in `隐式游标`

BEGIN

FOR RS IN (SELECT column_1 FROM table_name) LOOP

dbms_output.put_line(RS.column_1);

END LOOP;

END;

— RS 必须是单个字段

DECLARE

CURSOR RS IS SELECT column_1 FROM table_name;

column_1 VARCHAR2(50);

BEGIN

OPEN RS;

LOOP

FETCH RS INTO column_1;

EXIT WHEN RS%NOTFOUND;

dbms_output.put_line(column_1);

END LOOP;

CLOSE RS;

END;

/

— bulk collect显示游标

DECLARE

CURSOR RS IS SELECT column_1 FROM table_name;

column_1_array dbms_sql.Varchar2_Table;

BEGIN

OPEN RS;

FETCH RS BULK COLLECT INTO column_1_array;

FOR i IN 1 .. column_1_array.count LOOP

dbms_output.put_line(column_1_array(i));

END LOOP;

CLOSE RS;

END;

— bulk collect 显示游标 + limit

DECLARE

CURSOR RS IS SELECT column_1 FROM table_name;

column_1_array dbms_sql.Varchar2_Table;

BEGIN

OPEN RS;

LOOP

FETCH RS BULK COLLECT INTO column_1_array LIMIT 2;

EXIT WHEN RS%NOTFOUND;

FOR i IN 1 .. column_1_array.count LOOP

dbms_output.put_line(column_1_array(i));

END LOOP;

END LOOP;

CLOSE RS;

END;

– select into 隐式游标

DECLARE

column_1 VARCHAR2(100);

BEGIN

SELECT zone_code INTO column_1 FROM table_name WHERE ROWNUM = 1;

dbms_output.put_line(column_1);

END;

– 动态sql,select into 隐式游标

DECLARE

column_1 VARCHAR2(50);

l_table VARCHAR2(200);

l_sql VARCHAR2(200);

BEGIN

l_table := ‘table_name’;

l_sql := ‘select column_1 from ‘ || l_table || ‘ where rownum = 1’;

EXECUTE IMMEDIATE l_sql INTO column_1;

dbms_output.put_line(column_1);

END;

– 动态sql select into 隐式游标 + bulk collect

DECLARE

column_1_array dbms_sql.Varchar2_Table;

l_table VARCHAR2(20);

l_sql VARCHAR2(100);

BEGIN

l_table := ‘table_name’;

l_sql := ‘select column_1 from ‘ || l_table;

EXECUTE IMMEDIATE l_sql BULK COLLECT INTO column_1_array;

FOR i IN 1 .. column_1_array .count LOOP

dbms_output.put_line(column_1_array(i));

END LOOP;

END;

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/16064.html

(0)
上一篇 2024-02-11 15:15
下一篇 2024-02-12 10:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信