ORACLE中的游标汇总
数 据库 2008-07-07 12:34:14 阅读1040 评论2 字号:大 中 小
游标(Cursor)
游标(Cursor):用来查询数据库,获取记录集合(结果集)的指针,可以让开发者一次访问一行结果集,在每条结果集上作操作。
游标可分为:
<!--[if !supportLists]-->l <!--[endif]-->静态游标:分为显式(explicit)游标和隐式(implicit)游标。
<!--[if !supportLists]-->l <!--[endif]-->REF游标:是一种引用类型,类似于指针。----也称为动态游标
1、静态游标 1.1显式 游标 定义格式: CURSOR 游标名 ( 参数 ) [返回值类型] IS Select 语句 例子
select * from employees where department_id = p_deptid; l_emp employees%rowtype; begin dbms_output.put_line('Getting employees from department 30');open emp_cur(30); loop fetch emp_cur into l_emp; exit when emp_cur%notfound; dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is'); dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name); end loop; close emp_cur; dbms_output.put_line('Getting employees from department 90'); open emp_cur(90); loop fetch emp_cur into l_emp; exit when emp_cur%notfound; dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is'); dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name); end loop; close emp_cur; end; / |
|
declare begin for my_dept_rec in ( select department_name, department_id from epartments) loop dbms_output.put_line(my_dept_rec.department_id || ' : ' || my_dept_rec.department_name); end loop; end; / |
|
尽量使 用隐式游标,避免编写附加的游标控制代码(声明,打开,获取,关闭),也不需要声明变量来保存从游标中获取的数据。
2、REF CURSOR游标
动态游标,在运行的时候才能确定游标使用的查询。可以分为:
<!--[if !supportLists]-->l <!--[endif]-->强类型(限制)(Strong REF CURSOR),规定返回类型
<!--[if !supportLists]-->l <!--[endif]-->弱类型(非限制)(Weak REF CURSOR),不规定返回类型,可以获取任何结果集。
定义格式: TYPE ref_cursor_name IS REF CURSOR [RETURN return_type] 例 如: Declare Type refcur_t is ref cursor; Type emp_refcur_t is ref cursor return employee%rowtype; Begin Null; End; / 强类型举例:
|
CREATE OR REPLACE PACKAGE emp_data AS TYPE empcurtyp IS REF CURSOR RETURN emp%rowtype; --定义Strong REF CURSOR PROCEDURE open_emp_cv(emp_cv IN OUT empcurtyp, choice INT); --根据不同的choice选择不同的CURSOR PROCEDURE retrieve_data(choice INT); --通过调用procedure open_emp_cv,返回指定的结果集。 END emp_data; |
--emp_cv作为传入/传出的CURSOR PARAMETER BEGIN IF choice = 1 THENOPEN emp_cv FOR SELECT * FROM emp WHERE empno < 7800; ELSIF choice = 2 THENOPEN emp_cv FOR SELECT * FROM emp WHERE SAL < 1000; ELSIF choice = 3 THENOPEN emp_cv FOR SELECT * FROM emp WHERE ename like 'J%'; END IF; END; PROCEDURE retrieve_data(choice INT) ISreturn_cv empcurtyp; --定义传入open_emp_cv的CURSOR变量 return_row emp%ROWTYPE; invalid_choice EXCEPTION; BEGIN --调用 procedure OPEN_EMP_CV open_emp_cv(return_cv, choice); IF choice = 1 THENDBMS_OUTPUT.PUT_LINE('EMPLOYEES with empno less t han7800'); ELSIF choice = 2 THENDBMS_OUTPUT.PUT_LINE('EMPLOYEES with salary less than 1000'); ELSIF choice = 3 THENDBMS_OUTPUT.PUT_LINE('EMPLOYEES with name starts with ''J'''); ELSERAISE invalid_choice; END IF; LOOPFETCH return_cv INTO return_row; EXIT WHEN return_cv%NOTFOUND; DBMS_OUTPUT.PUT_LINE(return_row.empno || '--' || return_row.ename || '--' || return_row.sal); END LOOP; EXCEPTIONWHEN invalid_choice THEN DBMS_OUTPUT.PUT_LINE('The CHOICE should be in one of (1,2,3)!'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Errors in procedure RETRIEVE_DATA!'); END; END emp_data; |
--参数return_cv为weak REF CURSOR,利用SYS_CURSOR来定义 /*使用SYS_REFCURSOR可以使变量any_cv跟任何weak REF CURSOR类型匹配。 */ begin if choice = 1 thenopen return_cv for 'select * from emp'; elsif choice = 2 thenopen return_cv for 'select * from dept'; end if; end open_cv; |
dbms_output.put_line('employee information'); open_cv(1,return_cv); --调用procedure open_cv; loop fetch return_cv into emp_rec; exit when return_cv%notfound; dbms_output.put_line(emp_rec.empno||'-'||emp_rec.ename||'-'||emp_rec.sal); end loop; elsif choice=2 thendbms_output.put_line('department information'); open_cv(2,return_cv);
loop fetch return_cv into dept_rec; exit when return_cv%notfound; dbms_output.put_line(dept_rec.deptno||'-'||dept_rec.dname||'-'||dept_rec.loc); end loop; elseraise invalid_choice; end if; exceptionwhen invalid_choice then dbms_output.put_line('The CHOICE should be one of 1 and 2!'); when others thendbms_output.put_line('Errors in procedure retrieve_data'); END retrieve_data;
|
/* 定义复杂类型 */ type empcurtyp is ref cursor; type idlist is table of emp.empno%type; type namelist is table of emp.ename%type; type sallist is table of emp.sal%type; /* 定义变量 */emp_cv empcurtyp; ids idlist; names namelist; sals sallist; row_cnt number; begin open emp_cv for select empno, ename, sal from emp; fetch emp_cv BULK COLLECT INTO ids, names, sals; --将字段成批放入变量中,此时变量是一个集合 close emp_cv;
for i in ids.first .. ids.last loop dbms_output.put_line(' || ids(i) || ' || names(i) ||' salary=' || sals(i)); end loop;
FORALL i IN ids.first .. ids.last insert into tab2 values (ids(i), names(i), sals(i)); commit; select count(*) into row_cnt from tab2; dbms_output.put_line('-----------------------------------'); dbms_output.put_line('The row number of tab2 is ' || row_cnt); end REF_BULK;
|
从技术底层看,两者是相同的。普通plsql cursor在定义时是“静态”的。而
Ref cursors可以动态打开。例如下面例子:
|
<!--[if !supportLists]-->l <!--[endif]-->rc根据逻辑动态打开;而游标c定义好了只有就无法修改了。
<!--[if !supportLists]-->l <!--[endif]-->ref cursor可以返回给客户端,cursor则不行。
<!--[if !supportLists]-->l <!--[endif]-->cursor可以是全局的global ,ref cursor则必须定义在过程或函数中。
<!--[if !supportLists]-->l <!--[endif]-->ref cursor可以在子程序间传递,cursor则不行。
<!--[if !supportLists]-->l <!--[endif]-->cursor中定义的静态sql比ref cursor效率高,所以ref cursor通常用在:向客户端返回结果集。