PL/SQL 中如何正确选择游标类型
2007-05-19 12:30:55 来源:WEB开发网核心提示: declarecursor c is select tname from tab ;l_tname_array dbms_sql.varchar2_table;beginopen c ;loopfetch c bulk collect intol_tname_array limit 10
declare
cursor c is select tname from tab ;
l_tname_array dbms_sql.varchar2_table;
begin
open c ;
loop
fetch c bulk collect into l_tname_array limit 10 ;
exit when c%notfound ;
for i in 1 .. l_tname_array.count loop
dbms_output.put_line(l_tname_array(i) );
end loop;
end loop;
close c;
end;
/
..
..
隐式游标相对于显式游标而言,指的是不需要事先Declare,也无须用open,fetch,close的等方法来操作,而是通过其它的方式来操作游标
B. select into隐式游标
代码:
declare
l_tname varchar2(100);
begin
select tname into l_tname from tab where rownum = 1 ;
dbms_output.put_line(l_tname);
end;
/
..
..
动态SQL 的 select into隐式游标
代码:
declare
l_tname varchar2(100);
l_table_name varchar2(100);
l_sql varchar2(200);
begin
l_table_name := 'TAB' ;
l_sql := 'select tname from '||l_table_name ||' where rownum = 1 ' ;
execute immediate l_sql into l_tname;
for i in 1 .. l_tname_array.count loop
dbms_output.put_line(l_tname_array(i) );
end loop;
end;
/
..
..
动态SQL 的 select into隐式游标 + Bulk Collect
代码:
declare
l_tname_array dbms_sql.varchar2_table;
l_table_name varchar2(100);
l_sql varchar2(200);
begin
l_table_name := 'TAB' ;
l_sql := 'select tname from '||l_table_name ;
execute immediate l_sql bulk collect into l_tname_array;
for i in 1 .. l_tname_array.count loop
dbms_output.put_line(l_tname_array(i) );
end loop;
end;
/
..
..
更多精彩
赞助商链接