What Is Implicit Cursor in Oracle
What Is Implicit Cursor in Oracle
PL/SQL creates an implicit cursor whenever an SQL statement is executed through the code, unless the code employs an explicit cursor. The developer does not explicitly declare the cursor, thus, known as implicit cursor. E.g.: In the following UPDATE statement, which gives everyone in the company a 20% raise, PL/SQL creates an implicit cursor to identify the set of rows in the table which would be affected. UPDATE emp SET salary = salary * 1.2;
What is the difference between REF Cursor & Normal Cursor in oracle?
REF cursor is typically used to return record set or a cursor from stored procedure. REF Cursor is basically a data type. It is normally declared as type r_cursor is REF CURSOR; REF cursor supports dynamic change of query. Normal cursor is a static cursor in which the query is assigned at design time and cant be changed at run time. OR Normal cursors fall under the category of static cursors while REF cursors are dynamic. This means that normal cursors can only be used again not defined. Ref cursors on the other hand can be changed. A Ref cursor can be passed from one procedure to another. A normal cursor cannot.
cursor sample (v_key varchar2) is select initcap(book_title) bk_title, sum(quantity) sales, author_key from book join sales using (book_key) join book_author using (book_key) where author_key = v_key group by initcap(book_title), author_key; Parameterized cursor: /*Create a table*/ create table Employee( ID VARCHAR2(4 BYTE)NOT NULL, First_Name VARCHAR2(10 BYTE) ); /*Insert some data*/ Insert into Employee (ID, First_Name) values (01,Harry); /*create cursor*/ declare cursor c_emp(cin_No NUMBER)is select count(*) from employee where id=cin_No; v_deptNo employee.id%type:=10; v_countEmp NUMBER; begin open c_emp (v_deptNo); fetch c_emp into v_countEmp; close c_emp; end; /*Using cursor*/ Open c_emp (10);
exit when pkg_Util.c_emp%NOTFOUND; DBMS_OUTPUT.put_line(pkg_Util.r_emp.first_Name); end loop; close pkg_Util.c_emp; end; end;
OR
Cursor variables are preferred over a cursor for following reasons: A cursor variable is not tied to a specific query. One can open a cursor variable for any query returning the right set of columns. Thus, more flexible than cursors. A cursor variable can be passed as a parameter. A cursor variable can refer to different work areas.
OR
They are easier to define as there is no need to specify a query statement. The query can also be specified dynamically at the opening time. Cursor variables are easier to open.
Advantages of a cursor
Cursors can be used to process query results row by row. Using cursors, you can get, put, and delete database records. Because the results are processed row by row, data in each row can be processed in a different way.
E.g.: /* Create the cursor type. */ TYPE company_curtype IS REF CURSOR RETURN company%ROWTYPE; /* Declare a cursor variable of that type. */ company_curvar company_curtype;
OR
A cursor variable is capable to get associated with different SELECT statements at run time. It is a reference type which is quite similar to pointer in C. In order to use cursor variable, it has to be declared first, and then the storage has to be allocated.
A cursor variable is a variable of REF CURSOR data type which is a pointer to a data structure resource. It connects to query statement result, similar to the CURSOR data type. To define cursor variable, you must decide which REF CURSOR data type to use. The REF CURSOR data type can be selected in 3 different ways:
By defining a specific REF CURSOR types using the TYPE ... RETURN statement. By defining a generic REF CURSOR type using the TYPE ... statement. By using the system defined SYS_REFCURSOR.