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

A cursor variable is associated with different statements at run time, which can hold different values at run time. Static cursors can only be associated with one run time query. A cursor variable is reference type (like a pointer in C).
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.

Showing Answers 1 - 3 of 3 Answers

NIKHIL

  • Oct 12th, 2011
 

A cursor is just like an pointer variable which processes in the memories. Cursors can be used to process multirecord set. There are two types of cursors implicit and explicit cursors.

DECLARE var_rows number(5);
BEGIN
UPDATE employee
SET salary = salary + 1000;
IF SQL%NOTFOUND THEN
dbms_output.put_line('None of the salaries where updated');
ELSIF SQL%FOUND THEN
var_rows := SQL%ROWCOUNT;
dbms_output.put_line('Salaries for ' || var_rows || 'employees are updated');
END IF;
END;


The above example is for the implicit cursor.

Explicit Cusrors always process in the following manner

1)Open the Cursor
2)Fetch the Cursor
3)Close the Cursor

Example for the Explicit Cursor

Declare
empid emp.empno%type,
Cursor emp_cur
select sal from emp where eno:=empid;
Begin
vsal Number(5)
Open cur_emp
loop
fetch cur_emp into vsal
Exit When SQL%NOTFOUND
DBMS_OUTPUT.PUT_LINE("SALARY IS:"||vsal);
End;
close cur_emp;
End;
/

Code
  1. DECLARE var_rows NUMBER(5);


  2.   UPDATE employee

  3.   SET salary = salary + 1000;

  4.   IF SQL%NOTFOUND THEN

  5.     DBMS_OUTPUT.put_line('None of the salaries where updated');

  6.   ELSIF SQL%FOUND THEN

  7.     var_rows := SQL%ROWCOUNT;

  8.     DBMS_OUTPUT.put_line('Salaries for ' || var_rows || 'employees are updated');

  9.   END IF;

  10. END;

  11.  

  12.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions