Using %ROWTYPE with cursors v17
Using the %ROWTYPE
attribute, you can define a record that contains fields corresponding to all columns fetched from a cursor or cursor variable. Each field takes on the data type of its corresponding column. The %ROWTYPE
attribute is prefixed by a cursor name or cursor variable name.
<record> <cursor>%ROWTYPE;
record
is an identifier assigned to the record. cursor
is an explicitly declared cursor in the current scope.
This example shows how you can use a cursor with %ROWTYPE
to get information about which employee works in which department:
CREATE OR REPLACE PROCEDURE emp_info IS CURSOR empcur IS SELECT ename, deptno FROM emp; myvar empcur%ROWTYPE; BEGIN OPEN empcur; LOOP FETCH empcur INTO myvar; EXIT WHEN empcur%NOTFOUND; DBMS_OUTPUT.PUT_LINE( myvar.ename || ' works in department ' || myvar.deptno ); END LOOP; CLOSE empcur; END;
The following is the output from this procedure:
EXEC emp_info;
Output
SMITH works in department 20 ALLEN works in department 30 WARD works in department 30 JONES works in department 20 MARTIN works in department 30 BLAKE works in department 30 CLARK works in department 10 SCOTT works in department 20 KING works in department 10 TURNER works in department 30 ADAMS works in department 20 JAMES works in department 30 FORD works in department 20 MILLER works in department 10