REF CURSOR和CURSOR

REF CURSOR

DECLARE
    TYPE TY_EMP_CUR IS REF CURSOR;
    V_Emp_Cur              TY_EMP_CUR;
    V_Id                           EMP.ID%TYPE;
BEGIN
    OPEN  V_Emp_Cur   FOR    SELECT ID FROM EMP;      --REF CURSOR的SELECT语句赋值位置,可以在逻辑模块中再次更改
    FETCH  V_Emp_Cur  INTO  V_Id;                                     --FETCH的作用的读取当前指针并移动到下一个指针目标
    WHILE  V_Emp_Cur%FOUND  LOOP
        DBMS_OUTPUT.PUT_LINE(V_Id);
        FETCH  V_Emp_Cur  INTO  V_Id;
    END LOOP;
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Other exceptions');
        CONTINUE;
END;

CURSOR

DECLARE
    CURSOR V_Emp_Cur IS SELECT ID FROM EMP;           --传统CURSOR的SELECT语句赋值位置,后期不可更改
    V_Id                              EMP.ID%TYPE;
BEGIN
    OPEN  V_Emp_Cur;
    FETCH  V_Emp_Cur  INTO  V_Id;                                     --FETCH的作用的读取当前指针并移动到下一个指针目标
    WHILE  V_Emp_Cur%FOUND  LOOP
        DBMS_OUTPUT.PUT_LINE(V_Id);
        FETCH  V_Emp_Cur  INTO  V_Id;
    END LOOP;
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Other exceptions');
        CONTINUE;
END;

 

总结:REF CURSOR和传统的CURSOR相比,传统的CURSOR只能在定义模块中给予该CURSOR相应的SELECT语句去完成定义,且不能后期在逻辑模块再次进行改变,用途很局限,除非SELECT语句不会变化;而REF CURSOR更像是定义一种类型,在定义模块中定义完TYPE和此TYPEVARIABLE,在逻辑模块中去给予SELECT语句,相当于赋值变量,用法灵活,更方便遍历、绑定变量等实际中应用比较多的操作。

转载于:https://www.cnblogs.com/JeromeZ/p/5337736.html

### Refcursor vs Cursor in PostgreSQL Differences and Usage In PostgreSQL, both `REFCURSOR` and cursors serve similar purposes but differ significantly in their implementation and use cases. #### Definition and Basic Characteristics A **cursor** allows retrieval of multiple rows from a query one at a time or in blocks. This feature is particularly useful when dealing with large datasets that cannot fit into memory all at once[^1]. Cursors can be declared within PL/pgSQL functions or procedures directly using the `DECLARE CURSOR` syntax. On the other hand, a **REFCURSOR** represents an abstract handle to a cursor rather than being a cursor itself. It acts more like a pointer or reference to a cursor object created elsewhere. Unlike regular cursors, `REFCURSOR`s do not require explicit declaration inside procedural code; instead, they get associated dynamically through function calls returning such references. #### Syntax Examples Here’s how each type gets defined: For standard cursors: ```sql BEGIN; DECLARE my_cursor SCROLL CURSOR FOR SELECT * FROM employees WHERE department_id = 50; FETCH ALL IN my_cursor; CLOSE my_cursor; COMMIT; ``` Using REFCURSORS involves creating stored procedures/functions that open cursors internally before passing back handles via OUT parameters. ```plpgsql CREATE OR REPLACE FUNCTION fetch_employee_data(dept integer, refcur refcursor) RETURNS refcursor AS $$ BEGIN OPEN refcur FOR SELECT * FROM employees WHERE department_id = dept; RETURN refcur; END; $$ LANGUAGE plpgsql; -- Calling the above-defined procedure would look something like this: BEGIN; SELECT fetch_employee_data(50, 'my_ref_cursor'); FETCH ALL IN "my_ref_cursor"; CLOSE "my_ref_cursor"; COMMIT; ``` #### Key Distinctions Between REF_CURSOR and Regular Cursors - **Scope**: Standard cursors exist only during transaction boundaries while `REFCURSOR`s persist beyond transactions since these act merely as pointers passed around between different parts of applications. - **Usage Contexts**: While direct manipulation (open/fetch/close) works fine for simple scripts or interactive sessions involving plain SQL commands, complex logic requiring dynamic queries benefits greatly from employing `REFCURSOR`. - **Portability Across Sessions/Connections**: Since `REFCURSOR` essentially serves as opaque identifiers pointing towards actual server-side resources allocated upon invocation, sharing across separate client connections becomes feasible without exposing underlying details about internal structures involved. #### Practical Implications on Application Design Choices Choosing whether to implement traditional cursors versus leveraging `REFCURSOR` depends largely on specific requirements surrounding data access patterns alongside architectural considerations regarding session management strategies employed throughout software stacks interfacing with relational databases managed under PostgreSQL environments. --related questions-- 1. How does Spring Batch address performance issues related to handling massive volumes of records? 2. What changes were made to alter column definitions specifically concerning storage characteristics in Oracle tables? 3. Can DelimitedLineTokenizer process CSV files containing quoted strings properly?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值