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

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

提交回复
热议问题