What is the difference between explicit and implicit cursors in Oracle?

前端 未结 16 1925
温柔的废话
温柔的废话 2020-12-08 07:41

I am a bit rusty on my cursor lingo in PL/SQL. Anyone know this?

相关标签:
16条回答
  • 2020-12-08 08:09

    Every SQL statement executed by the Oracle database has a cursor associated with it, which is a private work area to store processing information. Implicit cursors are implicitly created by the Oracle server for all DML and SELECT statements.

    You can declare and use Explicit cursors to name the private work area, and access its stored information in your program block.

    0 讨论(0)
  • 2020-12-08 08:11

    These days implicit cursors are more efficient than explicit cursors.

    http://www.oracle.com/technology/oramag/oracle/04-sep/o54plsql.html

    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1205168148688

    0 讨论(0)
  • 2020-12-08 08:14

    In answer to the first question. Straight from the Oracle documentation

    A cursor is a pointer to a private SQL area that stores information about processing a specific SELECT or DML statement.

    0 讨论(0)
  • 2020-12-08 08:14

    With explicit cursors, you have complete control over how to access information in the database. You decide when to OPEN the cursor, when to FETCH records from the cursor (and therefore from the table or tables in the SELECT statement of the cursor) how many records to fetch, and when to CLOSE the cursor. Information about the current state of your cursor is available through examination of the cursor attributes.

    See http://www.unix.com.ua/orelly/oracle/prog2/ch06_03.htm for details.

    0 讨论(0)
  • 2020-12-08 08:15

    An explicit cursor is defined as such in a declaration block:

    DECLARE 
    CURSOR cur IS 
      SELECT columns FROM table WHERE condition;
    BEGIN
    ...
    

    an implicit cursor is implented directly in a code block:

    ...
    BEGIN
       SELECT columns INTO variables FROM table where condition;
    END;
    ...
    
    0 讨论(0)
  • 2020-12-08 08:15

    From a performance point of view, Implicit cursors are faster.

    Let's compare the performance between an explicit and implicit cursor:

    SQL> DECLARE
      2    l_loops  NUMBER := 100000;
      3    l_dummy  dual.dummy%TYPE;
      4    l_start  NUMBER;
      5    -- explicit cursor declaration
      6    CURSOR c_dual IS
      7      SELECT dummy
      8      FROM   dual;
      9  BEGIN
     10    l_start := DBMS_UTILITY.get_time;
     11    -- explicitly open, fetch and close the cursor
     12    FOR i IN 1 .. l_loops LOOP
     13      OPEN  c_dual;
     14      FETCH c_dual
     15      INTO  l_dummy;
     16      CLOSE c_dual;
     17    END LOOP;
     18
     19    DBMS_OUTPUT.put_line('Explicit: ' ||
     20                         (DBMS_UTILITY.get_time - l_start) || ' hsecs');
     21
     22    l_start := DBMS_UTILITY.get_time;
     23    -- implicit cursor for loop
     24    FOR i IN 1 .. l_loops LOOP
     25      SELECT dummy
     26      INTO   l_dummy
     27      FROM   dual;
     28    END LOOP;
     29
     30    DBMS_OUTPUT.put_line('Implicit: ' ||
     31                         (DBMS_UTILITY.get_time - l_start) || ' hsecs');
     32  END;
     33  /
    Explicit: 332 hsecs
    Implicit: 176 hsecs
    
    PL/SQL procedure successfully completed.
    

    So, a significant difference is clearly visible. Implicit cursor is much faster than an explicit cursor.

    More examples here.

    0 讨论(0)
提交回复
热议问题