游标
检索数据库中多条语句
结果集 next 指针
静态sql 动态 分配语句 ‘sql’
定义游标
declare
type v_cur is ref cursor;
声明游标
declare
v_c v_cur;
开启游标
open v_c for sql;
使用游标
fetch v_c into v_s;
关闭游标
begin
close v_c;
declare
type v_cur is ref cursor;
v_c v_cur;
v_sql varchar2(50);
v_test testA%rowtype;
begin
v_sql:='select * from testA';
open v_c for v_sql;
fetch v_c into v_test;
dbms_output.put_line(v_test.id||' '||v_test.name);
close v_c;
end;
/
遍历游标
v_c%found true/false ture 上一次fetch有数据返回
v_c%notfound true/false
%rowcount 处理了多少条数据
%isopen 游标是否是开启状态
declare
type v_cur is ref cursor;
v_c v_cur;
v_sql varchar2(50);
v_test testA%rowtype;
begin
v_sql:='select * from testA';
open v_c for v_sql;
loop
fetch v_c into v_test;
exit when v_c%notfound;
dbms_output.put_line(v_test.id||' '||v_test.name);
end loop;
close v_c;
end;
/
不需要开启 不需要关闭 不需要声明自变量 for 只用于静态游标
declare
cursor v_c is select * from testA;
begin
for v_test in v_c loop
dbms_output.put_line(v_test.id||' '||v_test.name);
end loop;
end;
/
动态sql 早期绑定 晚期绑定
begin
‘create table testC(
id number,
name varchar2(20)
)’
end;
/
begin
execute immediate 'create table testd(id number,name varchar2(20))';
execute immediate 'insert into testd values(1,''zhangsan'')';
end;
/
检索数据库中多条语句
结果集 next 指针
静态sql 动态 分配语句 ‘sql’
定义游标
declare
type v_cur is ref cursor;
声明游标
declare
v_c v_cur;
开启游标
open v_c for sql;
使用游标
fetch v_c into v_s;
关闭游标
begin
close v_c;
declare
type v_cur is ref cursor;
v_c v_cur;
v_sql varchar2(50);
v_test testA%rowtype;
begin
v_sql:='select * from testA';
open v_c for v_sql;
fetch v_c into v_test;
dbms_output.put_line(v_test.id||' '||v_test.name);
close v_c;
end;
/
遍历游标
v_c%found true/false ture 上一次fetch有数据返回
v_c%notfound true/false
%rowcount 处理了多少条数据
%isopen 游标是否是开启状态
declare
type v_cur is ref cursor;
v_c v_cur;
v_sql varchar2(50);
v_test testA%rowtype;
begin
v_sql:='select * from testA';
open v_c for v_sql;
loop
fetch v_c into v_test;
exit when v_c%notfound;
dbms_output.put_line(v_test.id||' '||v_test.name);
end loop;
close v_c;
end;
/
不需要开启 不需要关闭 不需要声明自变量 for 只用于静态游标
declare
cursor v_c is select * from testA;
begin
for v_test in v_c loop
dbms_output.put_line(v_test.id||' '||v_test.name);
end loop;
end;
/
动态sql 早期绑定 晚期绑定
begin
‘create table testC(
id number,
name varchar2(20)
)’
end;
/
begin
execute immediate 'create table testd(id number,name varchar2(20))';
execute immediate 'insert into testd values(1,''zhangsan'')';
end;
/