What is CURSOR in PL/SQL?
A Cursor is a pointer to this context area. Oracle creates context area for processing an SQL statement which contains all information about the statement.
PL/SQL allows the programmer to control the context area through the cursor. A cursor holds the rows returned by the SQL statement. The set of rows the cursor holds is referred as active set. These cursors can also be named so that they can be referred from another place of the code.
Opening Cursor
Opening the cursor will instruct the PL/SQL to allocate the memory for this cursor. It will make the cursor ready to fetch the records.
DECLARE
CURSOR EMPCUR IS SELECT * FROM EMPLOYEES;
EMPREC EMPLOYEES%ROWTYPE;
BEGIN
OPEN EMPCUR;
LOOP
FETCH EMPCUR INTO EMPREC;
DBMS_OUTPUT.PUT_LINE(EMPREC.LAST_NAME||' GETS '||EMPREC.SALARY);
EXIT WHEN EMPCUR%ROWCOUNT >= 10 OR EMPCUR%NOTFOUND;
END LOOP;
CLOSE EMPCUR;
END;
0 comments:
Post a Comment