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語句時,更容易理解為什么不允許這樣做.
總結
以上是生活随笔為你收集整理的oracle不使用游标,oracle – 为什么我们不能在动态SQL语句中使用强引用游标?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 有indexof函数吗,详解PH
- 下一篇: oracle登录时无效的参数,oracl