这里是一个强类型的引用游标的过程:
SQL> create or replace procedure p1 is
2 type dept_rc is ref cursor return dept%rowtype;
3 my_ref_cursor dept_rc;
4 begin
5 open my_ref_cursor for
6 select * from dept;
7 end;
8/
Procedure created.
SQL>
因为EMP记录的签名不匹配DEPT表的下面一条语句失败。
SQL> create or replace procedure p1 is
2 type dept_rc is ref cursor return dept%rowtype;
3 my_ref_cursor dept_rc;
4 begin
5 open my_ref_cursor for
6 select * from emp;
7 end;
8/
Warning: Procedure created with compilation errors.
SQL> show error
Errors for PROCEDURE P1:
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/5 PL/SQL: SQL Statement ignored
6/9 PLS-00382: expression is of wrong type
SQL>
但如果我们改变投影匹配DEPT表,然后我们再有成功:
SQL> create or replace procedure p1 is
2 type dept_rc is ref cursor return dept%rowtype;
3 my_ref_cursor dept_rc;
4 begin
5 open my_ref_cursor for
6 select deptno, ename, job from emp;
7 end;
8/
Procedure created.
SQL>
所以,为什么我们不能用一个强类型的REF光标使用动态SQL?
SQL> create or replace procedure p1 is
2 type dept_rc is ref cursor return dept%rowtype;
3 my_ref_cursor dept_rc;
4 begin
5 open my_ref_cursor for
6 'select * from dept';
7 end;
8/
Warning: Procedure created with compilation errors.
SQL> show error
Errors for PROCEDURE P1:
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/5 PL/SQL: Statement ignored
5/10 PLS-00455: cursor 'MY_REF_CURSOR' cannot be used in dynamic SQL
OPEN statement
SQL>
因为编译器无法解析动态SQL语句中的字符串。所以它不能断言查询投影中的列与数字匹配并且数据类型是引用游标的签名。因此它无法验证引用游标变量和查询之间的契约。当我们认为动态SQL语句可以从USER_TAB_COLUMNS上的一个查询中组合出来时,为什么不能这么做呢?
来源
2010-05-06 19:21:20
APC