About REF CURSOR queries

2-42 Oracle Reports Users Guide to Building Reports See also Section 4.13.4.2, Creating or editing a group filter

2.6.10 About REF CURSOR queries

A REF CURSOR query uses PLSQL to fetch data. Each REF CURSOR query is associated with a PLSQL function that returns a cursor value from a cursor variable. The function must ensure that the REF CURSOR is opened and associated with a SELECT statement that has a SELECT list that matches the type of the REF CURSOR. Oracle Reports supports both static and dynamic REF CURSORs. For example: Static REF CURSOR: open l_rc for SELECT FROM emp WHERE ename = KING; Dynamic REF CURSOR: l_query := SELECT empno, ename, sal, hiredate FROM emp WHERE 1-1; open l_rc for l_query; For a detailed example, see Chapter 41, Building a Paper Report with REF CURSORs . Usage notes ■ Oracle Reports supports only strongly typed REF CURSORs. For example: type c1 is REF CURSOR RETURN empROWTYPE; Note: The use of REF CURSOR queries in Oracle Reports 11g Release 1 11.1.1 requires that your database version is 10.1.0.5 for 10.1 or 10.2.0.2 for 10.2 or higher. Interoperability between a 11g Release 1 11.1.1 database or Oracle Forms Services client and a 10.110.2 database requires a minimum patchset level of 10.1.0.5 for 10.1 or 10.2.0.2 for 10.2. An attempt to reference a 10.1 or 10.2 PLSQL unit or view under the following circumstances will fail with a PLS-801[55916] error unless the 10.1 or 10.2 environment has been patched to the right level: ■ A PLSQL unit, anonymous block, trigger, call statement, or SQL statement on a 11g Release 1 11.1.1 database invokes a PLSQL unit on a 10.1 or 10.2 database across a database link. ■ A PLSQL unit, anonymous block, trigger, or call statement on a 11g Release 1 11.1.1 database references a view on a 10.1 or 10.2 database across a database link and the view directly or indirectly references a PLSQL function or an object type. ■ An Oracle Forms Services 11g Release 1 11.1.1 client invokes a PLSQL unit in a 10.1 or 10.2 database using RPC. Note: The SELECT statement must be explicitly set for dynamic REF CURSORs. Advanced Concepts 2-43 ■ When you make a REF CURSOR query the child in a data link, the link can only be a group to group link. It cannot be a column to column link. ■ If you use a stored program unit to implement REF CURSORs, you receive the added benefits that go along with storing your program units in the Oracle database. ■ You base a query on a REF CURSOR when you want to: – more easily administer SQL. – avoid the use of lexical parameters in your reports. – share data sources with other applications, such as Form Builder. – increase control and security. – encapsulate logic within a subprogram. Furthermore, if you use a stored program unit to implement REF CURSORs, you receive the added benefits that go along with storing your program units in the Oracle database. For more information about REF CURSORs and stored subprograms, refer to the PLSQL Users Guide and Reference. Examples Example 1: Package with REF CURSOR example This package spec defines a REF CURSOR type that could be referenced from a REF CURSOR query function. If creating this spec as a stored procedure in a tool such as SQLPlus, you would need to use the CREATE PACKAGE command. PACKAGE cv IS type comp_rec is RECORD deptno number, ename varchar10, compensation number; type comp_cv is REF CURSOR return comp_rec; END; Example 2: Package with REF CURSOR and function This package spec and body define a ref cursor type as well as a function that uses the REF CURSOR to return data. The function could be referenced from the REF CURSOR query, which would greatly simplify the PLSQL in the query itself. If creating this spec and body as a stored procedure in a tool such as SQLPlus, you would need to use the CREATE PACKAGE and CREATE PACKAGE BODY commands. PACKAGE cv IS 2-44 Oracle Reports Users Guide to Building Reports type comp_rec is RECORD deptno number, ename varchar10, compensation number; type comp_cv is REF CURSOR return comp_rec; function emprefcdeptno1 number return comp_cv; END; PACKAGE BODY cv IS function emprefcdeptno1 number return comp_cv is temp_cv cv.comp_cv; begin if deptno1 20 then open temp_cv for select deptno, ename, 1.25sal+nvlcomm,0 compensation from emp where deptno = deptno1; else open temp_cv for select deptno, ename, 1.15sal+nvlcomm,0 compensation from emp where deptno = deptno1; end if; return temp_cv; end; END; Example 3: REF CURSOR query This REF CURSOR query function would be coded in the query itself. It uses the cv.comp_cv REF CURSOR from the cv package to return data for the query. function DS_3RefCurDS return cv.comp_cv is temp_cv cv.comp_cv; begin if :deptno 20 then open temp_cv for select deptno, ename, 1.25sal+nvlcomm,0 compensation from emp where deptno = :deptno; else open temp_cv for select deptno, ename, 1.15sal+nvlcomm,0 compensation from emp where deptno = :deptno; end if; return temp_cv; end; Example 4: REF CURSOR query calling function This REF CURSOR query function would be coded in the query itself. It uses the cv.comp_cv REF CURSOR and the cv.emprefc function from the cv package to return data for the query. Because it uses the function from the cv package, the logic for the query resides mainly within the package. Query administrationmaintenance can be done at the package level for example, modifying SELECT clauses could be done by updating the package. You could also easily move the package to the database. Advanced Concepts 2-45 Note this example assumes you have defined a user parameter named deptno. function DS_3RefCurDS return cv.comp_cv is temp_cv cv.comp_cv; begin temp_cv := cv.emprefc:deptno; return temp_cv; end; See also Section 4.8.1.7, Creating a query: REF CURSOR Query tool

2.6.11 About DML and DDL