oracle不使用游标,oracle – 为什么我们不能在动态SQL语句中使用强引用游标?
這是一個(gè)帶有強(qiáng)類型引用游標(biāo)的過(guò)程:
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>
下一個(gè)語(yǔ)句失敗,因?yàn)镋MP記錄的簽名與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表,那么我們?cè)俅潍@得成功:
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>
那么,為什么我們不能使用帶有動(dòng)態(tài)SQL的強(qiáng)類型引用游標(biāo)?
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>
因?yàn)榫幾g器無(wú)法解析動(dòng)態(tài)SQL語(yǔ)句中的字符串.因此,它無(wú)法斷言查詢投影中的列與數(shù)字和數(shù)據(jù)類型匹配ref游標(biāo)的簽名.因此,它無(wú)法驗(yàn)證ref游標(biāo)變量和查詢之間的契約.當(dāng)我們考慮可以從USER_TAB_COLUMNS上的查詢匯編動(dòng)態(tài)SQL語(yǔ)句時(shí),更容易理解為什么不允許這樣做.
總結(jié)
以上是生活随笔為你收集整理的oracle不使用游标,oracle – 为什么我们不能在动态SQL语句中使用强引用游标?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php 有indexof函数吗,详解PH
- 下一篇: 建表时数据库建议使用 utf8mb4字符