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

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

    An implicit cursor is one created "automatically" for you by Oracle when you execute a query. It is simpler to code, but suffers from

    • inefficiency (the ANSI standard specifies that it must fetch twice to check if there is more than one record)
    • vulnerability to data errors (if you ever get two rows, it raises a TOO_MANY_ROWS exception)

    Example

    SELECT col INTO var FROM table WHERE something;
    

    An explicit cursor is one you create yourself. It takes more code, but gives more control - for example, you can just open-fetch-close if you only want the first record and don't care if there are others.

    Example

    DECLARE   
      CURSOR cur IS SELECT col FROM table WHERE something; 
    BEGIN
      OPEN cur;
      FETCH cur INTO var;
      CLOSE cur;
    END;
    
    0 讨论(0)
  • 2020-12-08 08:30

    1.CURSOR: When PLSQL issues sql statements it creates private work area to parse & execute the sql statement is called cursor.

    2.IMPLICIT: When any PL/SQLexecutable block issues sql statement. PL/SQL creates implicit cursor and manages automatically means implcit open & close takes place. It used when sql statement return only one row.It has 4 attributes SQL%ROWCOUNT, SQL%FOUND, SQL%NOTFOUND, SQL%ISOPEN.

    3.EXPLICIT: It is created & managed by the programmer. It needs every time explicit open,fetch & close. It is used when sql statement returns more than one row. It has also 4 attributes CUR_NAME%ROWCOUNT, CUR_NAME%FOUND, CUR_NAME%NOTFOUND, CUR_NAME%ISOPEN. It process several rows by using loop. The programmer can pass the parameter too to explicit cursor.

    • Example: Explicit Cursor

     

    declare 
       cursor emp_cursor 
       is 
       select id,name,salary,dept_id 
       from employees; 
       v_id employees.id%type; 
       v_name employees.name%type; 
       v_salary employees.salary%type; 
       v_dept_id employees.dept_id%type; 
       begin 
       open emp_cursor; 
       loop 
       fetch emp_cursor into v_id,v_name,v_salary,v_dept_id; 
       exit when emp_cursor%notfound;
       dbms_output.put_line(v_id||', '||v_name||', '||v_salary||','||v_dept_id); 
       end loop;                    
       close emp_cursor; 
       end;
    
    0 讨论(0)
  • 2020-12-08 08:31

    Implicit cursors require anonymous buffer memory.

    Explicit cursors can be executed again and again by using their name.They are stored in user defined memory space rather than being stored in an anonymous buffer memory and hence can be easily accessed afterwards.

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

    Explicit...

    cursor foo is select * from blah; begin open fetch exit when close cursor yada yada yada

    don't use them, use implicit

    cursor foo is select * from blah;

    for n in foo loop x = n.some_column end loop

    I think you can even do this

    for n in (select * from blah) loop...

    Stick to implicit, they close themselves, they are more readable, they make life easy.

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