【oracle】补充 cursor 基本例子
1118-02補充 cursor 基本例子
查看 共享的命令:
cmd
fsmgmt.msc
?
/*
cursor
1.參數列表,is <select_statement> 是否含有 變量
2.接收游標值
2.1 標量類型變量
2.2 記錄類型變量
2.3 for循環中的循環計數變量
3.游標屬性
3.1 顯式游標,ref cursor 語法: 游標名%游標屬性
3.2 隱式游標 語法: SQL%游標屬性
*/
set serveroutput on;
/*cursor,無參,接收游標值:1.標量類型變量*/
declare
cursor v_cur is
select serial_no,type_code,brand_code,brand_name from t_sales;
vi_serial_no integer;
vv_type_code varchar2(30);
vv_brand_code varchar2(30);
vv_brand_name varchar2(30);
begin
open v_cur;
loop
fetch v_cur into vi_serial_no,vv_type_code,vv_brand_code,vv_brand_name;
exit when v_cur%notfount;
dbms_output.put_line( vi_serial_no||','||vv_type_code||','||vv_brand_code||','||vv_brand_name );
end loop;
close v_cur;
exception
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
set serveroutput on;
/*cursor,無參,接收游標值:2.記錄類型變量*/
declare
cursor v_cur is
select serial_no,type_code,brand_code,brand_name from t_sales;
type t_record is record (
serial_no integer,
type_code varchar2(30),
brand_code varchar2(30),
brand_name varchar2(30)
);
v_record t_record; --記錄類型 的顯式定義
--v_record t_sales%rowtype; --記錄類型 的隱式定義(table)
-- v_record v_t_sales%rowtype; --記錄類型 的隱式定義(view)
--v_record v_cur%rowtype; --記錄類型 的游標定義
begin
open v_cur;
loop
fetch v_cur into v_record;
-- fetch v_cur into v_record.serial_no,v_record.type_code,v_record.brand_code,v_record.brand_name;
exit when v_cur%notfount;
dbms_output.put_line( vi_serial_no||','||vv_type_code||','||vv_brand_code||','||vv_brand_name );
end loop;
close v_cur;
exception
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
?
set serveroutput on;
/*cursor,無參,接收游標值:3.for循環中的循環計數標量*/
declare
cursor v_cur is
select serial_no,type_code,brand_code,brand_name from t_sales;
begin
for i_row in v_cur loop
dbms_output.put_line( i_row.serial_no||','||i_row.type_code||','||i_row.brand_code||','||i_row.brand_name );
end loop;
exception
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
set serveroutput on;
/*cursor,有參,無變量*/
declare
cursor v_cur(vp_serial_no integer default 5) is
select serial_no,type_code,brand_code,brand_name from t_sales where serial_no <= vp_serial_no;
begin
for i_row in v_cur(3) loop
dbms_output.put_line( i_row.serial_no||','||i_row.type_code||','||i_row.brand_code||','||i_row.brand_name );
end loop;
exception
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
set serveroutput on;
/*cursor,有參,有變量*/
declare
vv_type_code varchar2(30);
cursor v_cur(vp_serial_no integer default 5) is
select serial_no,type_code,brand_code,brand_name from t_sales where serial_no <= vp_serial_no and type_code = upper(vv_type_code);
v_record v_cur%rowtype;
begin
vv_type_code := 'T01'; --open前 賦值
--open v_cur; --參數列表 有默認值的游標,open時可以不帶參數列表
open v_cur(3);
loop
fetch v_cur into v_record;
exit when v_cur%notfount;
dbms_output.put_line( v_record.serial_no||','||v_record.type_code||','||v_record.brand_code||','||v_record.brand_name );
end loop;
close v_cur;
exception
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
set serveroutput on;
/*游標屬性:1.顯式游標,ref cusor*/
declare
cursor v_cur is
select serial_no,type_code,brand_code,brand_name from t_sales;
v_record v_cur%rowtype;
begin
if not (v_cur%isopen) then
open v_cur;
end if;
fetch v_cur into v_record;
while v_cur%found loop
dbms_output.put_line( v_cur%rowcount||':'||v_record.serial_no||','||v_record.type_code||','||v_record.brand_code||','||v_record.brand_name );
fetch v_cur into v_record;
end loop;
exception
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
set serveroutput on;
/*游標屬性: 2.隱式游標*/
declare
type t_record is record (
serial_no integer,
type_code varchar2(30),
brand_code varchar2(30),
brand_name varchar2(30)
);
v_record t_record;
begin
select serial_no,type_code,brand_code,brand_name into v_record from t_sales where serial_no = 1;
dbms_output.put_line(to_char(SQL%rowcount));
update t_sales set brand_name = brand_name||'_01';
dbms_output.put_line(to_char(SQL%rowcount));
--commit;
rollback;
dbms_output.put_line(to_char(SQL%rowcount));
exception
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
/* ref cursor
定義:
1.select語句
2.sql字符串
*/
set serveroutput on;
/*ref cursor,指定游標語句:1.select語句*/
declare
type t_ref_cursor is ref cursor;
v_ref_cur t_ref_cursor;
vi_serial_no integer := 5;
v_record t_sales%rowtype;
begin
open v_ref_cur for
select serial_no,type_code,brand_code,brand_name into v_record from t_sales where serial_no <= vi_serial_no;
loop
fetch v_ref_cur into v_record;
exit when v_reg_cur%notfound;
dbms_output.put_line(v_record.serial_no||','||v_record.type_code||','||v_record.brand_code||','||v_record.brand_name );
end loop;
exception
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
set serveroutput on;
/*ref cursor,指定游標語句:2.sql字符串*/
declare
type t_ref_cursor is ref cursor;
v_ref_cur t_ref_cursor;
vi_serial_no integer := 5;
v_record t_sales%rowtype;
vv_sql varchar2(500);
begin
vi_serial_no := 3;
/*使用 普通變量
vv_sql := ' select serial_no,type_code,brand_code,brand_name into v_record from t_sales where serial_no <= '||vi_serial_no ;
open v_ref_cur for vv_sql;
*/
/*使用 綁定變量*/
vv_sql := ' select serial_no,type_code,brand_code,brand_name into v_record from t_sales where serial_no <= :serial_no' ;
open v_ref_cur for vv_sql using vi_serial_no; --open時指定綁定變量的值
loop
fetch v_ref_cur into v_record;
exit when v_reg_cur%notfound;
dbms_output.put_line(v_record.serial_no||','||v_record.type_code||','||v_record.brand_code||','||v_record.brand_name );
end loop;
exception
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
轉載于:https://www.cnblogs.com/greenZ/p/8721850.html
總結
以上是生活随笔為你收集整理的【oracle】补充 cursor 基本例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: web开发快速提高工作效率的一些资源
- 下一篇: css深入浅出 宽度和高度