Vous êtes sur la page 1sur 6

declare type r_cursor is REF CURSOR; c_emp r_cursor; type rec_emp is record ( name varchar2(20), sal number(6) ); er rec_emp;

begin open c_emp for select ename,sal from emp; loop fetch c_emp into er; exit when c_emp%notfound; dbms_output.put_line(er.name || ' - ' || er.sal); end loop; close c_emp; end; The most confusing aspect from the above program is the following: type rec_emp is record ( name varchar2(20), sal number(6) ); The above defines a new data type named "rec_emp" (just like %ROWTYPE with limited specified fields) which can hold two fields, namely "name" and "sal." er rec_emp; The above statement declares a variable "er" based on the datatype "rec_emp." This means that "er" internally contains the fields "name" and "job." fetch c_emp into er; The above statement pulls out a row of information (in this case "ename" and "sal") and places the same into the fields "name" and "sal" of the variable "er." Finally, I display both of those values using the following statement: dbms_output.put_line(er.name || ' - ' || er.sal);

Working with more than one query with the same REF CURSOR - Oracle
As defined earlier, a REF CURSOR can be associated with more than one SELECT statement at run-time. Before associating a new SELECT statement, we need to close the CURSOR. Let us have an example as follows: declare type r_cursor is REF CURSOR; c_emp r_cursor; type rec_emp is record ( name varchar2(20), sal number(6) ); er rec_emp; begin open c_emp for select ename,sal from emp where deptno = 10; dbms_output.put_line('Department: 10'); dbms_output.put_line('--------------'); loop fetch c_emp into er; exit when c_emp%notfound; dbms_output.put_line(er.name || ' - ' || er.sal); end loop; close c_emp; open c_emp for select ename,sal from emp where deptno = 20; dbms_output.put_line('Department: 20'); dbms_output.put_line('--------------'); loop fetch c_emp into er; exit when c_emp%notfound; dbms_output.put_line(er.name || ' - ' || er.sal); end loop; close c_emp; end; n the above program, the skeleton looks like the following: declare . . Begin

. . open c_emp for select ename,sal from emp where deptno = 10; . . fetch c_emp into er; . . close c_emp; . . open c_emp for select ename,sal from emp where deptno = 20; . . fetch c_emp into er; . . close c_emp; . . end; From the above skeleton, you can easily understand that every CURSOR is opened, used and closed before opening the same with the next SELECT statement. Working with REF CURSOR inside loops Sometimes, it may be necessary for us to work with REF CURSOR within loops. Let us consider the following example: declare type r_cursor is REF CURSOR; c_emp r_cursor; type rec_emp is record ( name varchar2(20), sal number(6) ); er rec_emp; begin for i in (select deptno,dname from dept) loop open c_emp for select ename,sal from emp where deptno = i.deptno; dbms_output.put_line(i.dname); dbms_output.put_line('--------------');

loop fetch c_emp into er; exit when c_emp%notfound; dbms_output.put_line(er.name || ' - ' || er.sal); end loop; close c_emp; end loop; end; As you can observe from the above program, I implemented a FOR loop as follows: for i in (select deptno,dname from dept) loop . . end loop; The above loop iterates continuously for each row of the "dept" table. The details of each row in "dept" (like deptno, dname etc.) will be available in the variable "i." Using that variable (as part of the SELECT statement), I am working with REF CURSOR as follows: open c_emp for select ename,sal from emp where deptno = i.deptno; The rest of the program is quite commonplace.

Dealing with REF CURSOR in the subprograms of a PL/SQL block - Oracle


Sub-programs can also be called sub-routines. These are nothing but the divisions of the main program. These divisions are named and are executed when they are called by name from the main program. They will not get executed unless they are called. The following is an example: declare type r_cursor is REF CURSOR; c_emp r_cursor; type rec_emp is record ( name varchar2(20), sal number(6)

); er rec_emp; procedure PrintEmployeeDetails is begin loop fetch c_emp into er; exit when c_emp%notfound; dbms_output.put_line(er.name || ' - ' || er.sal); end loop; end; begin for i in (select deptno,dname from dept) loop open c_emp for select ename,sal from emp where deptno = i.deptno; dbms_output.put_line(i.dname); dbms_output.put_line('--------------'); PrintEmployeeDetails; close c_emp; end loop; end; In the above program, the sub-routine is named "PrintEmployeeDetails." You can observe that I am executing (or calling) the sub-routine from within the loop as follows: for i in (select deptno,dname from dept) loop . . PrintEmployeeDetails; . . end loop; According to the above loop, the sub-routine gets executed for every iteration, which displays the employee information for the respective department. Passing REF CURSOR as parameters to sub-programs In the previous section, we already started working with sub-programs (or sub-routines). In this section, I shall extend the same with the concept of "parameters" (or arguments). Every subprogram (or sub-routine) can accept values passed to it in the form of "parameters" (or arguments). Every parameter is very similar to a variable, but gets declared as part of a subprogram. Let us consider the following program:

declare type r_cursor is REF CURSOR; c_emp r_cursor; type rec_emp is record ( name varchar2(20), sal number(6) ); procedure PrintEmployeeDetails(p_emp r_cursor) is er rec_emp; begin loop fetch p_emp into er; exit when p_emp%notfound; dbms_output.put_line(er.name || ' - ' || er.sal); end loop; end; begin for i in (select deptno,dname from dept) loop open c_emp for select ename,sal from emp where deptno = i.deptno; dbms_output.put_line(i.dname); dbms_output.put_line('--------------'); PrintEmployeeDetails(c_emp); close c_emp; end loop; end;

Vous aimerez peut-être aussi