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

前端 未结 16 1927
温柔的废话
温柔的废话 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:15

    A cursor is a SELECTed window on an Oracle table, this means a group of records present in an Oracle table, and satisfying certain conditions. A cursor can SELECT all the content of a table, too. With a cursor you can manipulate Oracle columns, aliasing them in the result. An example of implicit cursor is the following:

    BEGIN
       DECLARE
          CURSOR C1
          IS
             SELECT DROPPED_CALLS FROM ALARM_UMTS;
    
          C1_REC   C1%ROWTYPE;
       BEGIN
          FOR C1_REC IN C1
          LOOP
             DBMS_OUTPUT.PUT_LINE ('DROPPED CALLS: ' || C1_REC.DROPPED_CALLS);
          END LOOP;
       END;
    END;
    /
    

    With FOR ... LOOP... END LOOP you open and close the cursor authomatically, when the records of the cursor have been all analyzed.

    An example of explicit cursor is the following:

    BEGIN
       DECLARE
          CURSOR C1
          IS
             SELECT DROPPED_CALLS FROM ALARM_UMTS;
    
          C1_REC   C1%ROWTYPE;
       BEGIN
          OPEN c1;
    
          LOOP
             FETCH c1 INTO c1_rec;
    
             EXIT WHEN c1%NOTFOUND;
    
             DBMS_OUTPUT.PUT_LINE ('DROPPED CALLS: ' || C1_REC.DROPPED_CALLS);
          END LOOP;
    
          CLOSE c1;
       END;
    END;
    /
    

    In the explicit cursor you open and close the cursor in an explicit way, checking the presence of records and stating an exit condition.

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

    In PL/SQL, A cursor is a pointer to this context area. It contains all the information needed for processing the statement.

    Implicit Cursors: Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there is no explicit cursor for the statement. Programmers cannot control the implicit cursors and the information in it.

    Explicit Cursors: Explicit cursors are programmer-defined cursors for gaining more control over the context area. An explicit cursor should be defined in the declaration section of the PL/SQL Block. It is created on a SELECT Statement which returns more than one row.

    The syntax for creating an explicit cursor is:

    CURSOR cursor_name IS select_statement; 
    
    0 讨论(0)
  • 2020-12-08 08:16

    Google is your friend: http://docstore.mik.ua/orelly/oracle/prog2/ch06_03.htm

    PL/SQL issues an implicit cursor whenever you execute a SQL statement directly in your code, as long as that code does not employ an explicit cursor. It is called an "implicit" cursor because you, the developer, do not explicitly declare a cursor for the SQL statement.

    An explicit cursor is a SELECT statement that is explicitly defined in the declaration section of your code and, in the process, assigned a name. There is no such thing as an explicit cursor for UPDATE, DELETE, and INSERT statements.

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

    As stated in other answers, implicit cursors are easier to use and less error-prone.

    And Implicit vs. Explicit Cursors in Oracle PL/SQL shows that implicit cursors are up to two times faster than explicit ones too.

    It's strange that no one had yet mentioned Implicit FOR LOOP Cursor:

    begin
      for cur in (
        select t.id from parent_trx pt inner join trx t on pt.nested_id = t.id
        where t.started_at > sysdate - 31 and t.finished_at is null and t.extended_code is null
      )
      loop
        update trx set finished_at=sysdate, extended_code = -1 where id = cur.id;
        update parent_trx set result_code = -1 where nested_id = cur.id;
      end loop cur;
    end;
    

    Another example on SO: PL/SQL FOR LOOP IMPLICIT CURSOR.

    It's way more shorter than explicit form.

    This also provides a nice workaround for updating multiple tables from CTE.

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

    An explicit cursor is one you declare, like:

    CURSOR my_cursor IS
      SELECT table_name FROM USER_TABLES
    

    An implicit cursor is one created to support any in-line SQL you write (either static or dynamic).

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

    Implicit cursor returns only one record and are called automatically. However, explicit cursors are called manually and can return more than one record.

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