oracle open for using的用法,oracle OPEN FOR [USING] 语句
目的:
和ref cursor配合使用, 可以將游標變量分配給不同的SQL (而不是在declare中把游標給定死), 增加處理游標的靈活性
語法:
declare
type type_cursor is ref cursor [return 記錄類型]; --使用 ref cursor 才能把游標分配給不同的SQL,return不能用在動態(tài)SQL中
v_cursor type_cursor ;
begin
OPEN v_cursor FOR select first_name, last_name from student;
OPEN v_cursor FOR ' select first_name,last_name from student where zip = :1 '
USING 綁定變量1;
open
靜態(tài)SQL
cursor
cursor c1 is
open c1; fetch ... into ... ; close c1;
open for
靜態(tài)SQL
ref cursor
type t_1 is ref cursor;
c2 t_1 ;
openc2 for;
open for using
動態(tài)SQL
type t_1 is ref cursor;
c2 t_1 ;
openc2 for using ... ;
例子1:
declare
type student_cur_type is ref CURSOR RETURN test_stu%ROWTYPE; --聲明ref cursor類型, return類型固定
v_first_name test_stu.first_name%TYPE;
v_last_name test_stu.last_name%TYPE;
cur_stud student_cur_type;
begin
open cur_stud for select first_name,last_name from student ; --帶return的ref cursor只能用在靜態(tài)sql中
loop
fetch cur_stud into v_first_name, v_last_name;
exit when cur_stud%NOTFOUND;
dbms_output.put_line(v_first_name || ' ' || v_last_name);
end loop;
close cur_stud;
end;
例子2:
declare
v_zip varchar2(5) := '&sv_zip';
v_first_name varchar2(25);
v_last_name varchar2(25);
type student_cur_type is ref cursor; --聲明ref cursor類型
student_cur student_cur_type; --student_cur是游標變量 / student_cur_type 是引用游標類型
begin
--2打開游標變量,讓它指向一個動態(tài)select查詢的結(jié)果集 ; 就是使用open for語句代替一般的open語句代開游標
open student_cur for 'select first_name,last_name ' from student where zip = :1'
using v_zip;
loop
fetch student_cur into v_first_name, v_last_name;
exit when student_cur%NOTFOUND;
dbms_output.put_line(v_first_name || ' ' || v_last_name);
end loop;
close student_cur;
end;
轉(zhuǎn)載自:https://blog.csdn.net/crzzyracing/article/details/75336196
總結(jié)
以上是生活随笔為你收集整理的oracle open for using的用法,oracle OPEN FOR [USING] 语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle编写备份数据库代码,orac
- 下一篇: Instagram 宣布放弃直播购物,M