加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle – 为什么我们不能在动态SQL语句中使用强引用游标?

发布时间:2020-12-12 13:49:48 所属栏目:百科 来源:网络整理
导读:我试图使用强大的ref cur与动态sql语句但它发出错误,但当我使用弱光标它工作,请解释是什么原因,请 向我转发oracle服务器架构师的任何链接,其中包含有关如何在Oracle服务器中完成编译和解析的事项.这是代码中的错误. ERROR at line 6:ORA-06550: line 6,colum
我试图使用强大的ref cur与动态sql语句但它发出错误,但当我使用弱光标它工作,请解释是什么原因,请
向我转发oracle服务器架构师的任何链接,其中包含有关如何在Oracle服务器中完成编译和解析的事项.这是代码中的错误.
ERROR at line 6:
ORA-06550: line 6,column 7:
PLS-00455: cursor 'EMP_REF_CUR' cannot be used in dynamic SQL OPEN statement
ORA-06550: line 6,column 2:
PL/SQL: Statement ignored

declare
      type ref_cur_type IS REF CURSOR RETURN employees%ROWTYPE; --Creating a strong REF cursor,employees is a table
     emp_ref_cur ref_cur_type;
     emp_rec employees%ROWTYPE;
BEGIN      
   OPEN emp_ref_cur FOR 'SELECT * FROM employees';
           LOOP
                   FETCH emp_ref_cur INTO emp_rec;
                   EXIT WHEN emp_ref_cur%NOTFOUND;
           END lOOP;       
END;
这是一个带有强类型引用游标的过程:
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语句时,更容易理解为什么不允许这样做.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读