oracle不使用游标,oracle – 为什么我们不能在动态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 /

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>

那么,为什么我们不能使用带有动态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语句中的字符串.因此,它无法断言查询投影中的列与数字和数据类型匹配ref游标的签名.因此,它无法验证ref游标变量和查询之间的契约.当我们考虑可以从USER_TAB_COLUMNS上的查询汇编动态SQL语句时,更容易理解为什么不允许这样做.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值