You are on page 1of 6

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 a SELECT FOR UPDATE cursor represent?


The result of a PREPARE statement is a statement identifier. It is a data structure that represents the prepared statement text. To declare a cursor for the statement text, we associate +- a cursor with the statement identifier. You can associate a sequential cursor with any prepared SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement. You cannot associate a scroll cursor with a prepared INSERT statement or with a SELECT statement that was prepared to include a FOR UPDATE clause. The SELECT FOR UPDATE clause in the cursor declaration is a convenient way of modifying the rows that have been retrieved by the cursor.

What WHERE CURRENT OF clause does in a cursor?


PL/SQL provides the WHERE CURRENT OF clause for both UPDATE and DELETE statements inside a cursor.This allows you to easily make changes to the most recently fetched row of data. Syntax: UPDATE table_name SET set_clause WHERE CURRENT OF cursor_name; Notice that the WHERE CURRENT OF clause references the cursor and not the record into which the next fetched row is deposited.

Can you pass a parameter to a cursor?


PL/SQL also allows you to pass parameters into cursors. It eases your work because: - A parameter makes the cursor more reusable. - A parameter avoids scoping problems. However, you should pass parameters when you are goint to use it at more then one place and when there are hoing to be different values for the same WHERE statement.

Explain the functioning of CURSOR FOR LOOP with example.


Lets have a look at what Cursors are before going to the Cursor FOR loop. A Cursor is a PL/SQL construct and accesses the stored information in a named work area. There are 2 types of cursors: Implicit: queries that return only one row Explicit: can be declared by us for the queries that return more than one row. e.g. DECLARE CURSOR cursor_1 IS SELECT roll_no, student_name FROM student WHERE grade = 4; A PL/SQL program opens a cursor, processes rows returned by a query, then closes the cursor. This can be done with the help of: OPEN, FETCH, and CLOSE statements Cursor FOR Loops Instead of using OPEN, FETCH, and CLOSE statements, coding can be simplified by using FOR loops. A cursor FOR loop opens a cursor, repeatedly fetches rows of values from the result set into fields in the record, then closes the cursor when all rows have been processed. In the example below, the cursor FOR loop implicitly declares stud_record as a record: DECLARE CURSOR cursor_1 IS SELECT student_name, birthdate FROM student; ... BEGIN FOR stud_record IN cuesor_1 LOOP ... ... END LOOP;

Define Simple/Explicit , Parametric and Internal/Implicit cursor.


A Cursor is a PL/SQL construct and accesses the stored information in a named work area. There are 2 types of cursors: Implicit: queries that return only one row Explicit: can be declared by us for the queries that return more than one row.

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.

What is use of a cursor variable in oracle? How it is defined?


A cursor variable works like pointer in C. It is used to hold address of an item rather than the item itself. Cursor variables can be used to hold different values at run time. They can be used to pass query result sets between PL/SQL stored subprograms. Declaring a cursor variable: TYPE type_name IS REF CURSOR RETURN return_type type_name is the name of the reference type return_type is a record type indicating the types of the select list that will eventually be returned by the cursor variable.

Can you pass a parameter to a cursor? Explain with an explain.


A cursor can have a parameter in the IN mode. Example: The cursor below accepts a parameter of data type varchar2.

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);

What is a package cursor?


A Package that returns a Cursor type is a package cursor. Eg: Create or replace package pkg_Util is cursor c_emp is select * from employee; r_emp c_emp%ROWTYPE; end; /*Another package using this package*/ Create or replace package body pkg_aDifferentUtil is procedure p_printEmps is begin open pkg_Util.c_emp; loop fetch pkg_Util.c_emp into pkg_Util.r_emp;

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;

Explain why cursor variables are easier to use than cursors.


A cursor variable is actually a pointer pointing to a queries result set. Using a cursor variable, each time a new result set is created by a query, cursor variable can be used to point the same. This improves the performance and streamlines the code. Being a variable, you can easily pass it as a parameter to a function and use it in assignment operations.

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.

What are the drawbacks of a cursor?



Implicit cursors are less efficient than explicit cursors Implicit cursors are more vulnerable to data errors Implicit cursors provide less programmatic control

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.

What is a cursor variable?


In case of a cursor, Oracle opens an anonymous work area that stores processing information. This area can be accessed by cursor variable which points to this area. One must define a REF CURSOR type, and then declare cursor variables of that type to do so.

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.

You might also like