为什么我们不能在动态 SQL 语句中使用强引用游标?
我正在尝试将强引用 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;
I am trying to use a strong ref cur with dynamic sql statment but it is giving out an error,but when i use weak cursor it works,Please explain what is the reason and please
forward me any link of oracle server architect containing matter about how compilation and parsing is done in Oracle server. THIS is the error along with code.
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一个带有强类型引用游标的过程:
下一条语句失败,因为 EMP 记录的签名与 DEPT 表的签名不匹配。
但是,如果我们更改投影以匹配 DEPT 表,那么我们再次成功:
那么,为什么我们不能将强类型引用游标与动态 SQL 一起使用呢?
因为编译器无法解析动态SQL语句中的字符串。因此它无法断言查询投影中的列在数量和数据类型上与引用游标的签名匹配。因此,它无法验证引用游标变量和查询之间的约定。当我们考虑到动态 SQL 语句可以从 USER_TAB_COLUMNS 上的查询组装而成时,就更容易理解为什么不允许这样做。
Here is a procedure with a strongly-typed ref cursor:
This next statement fails because the signature of the EMP record doesn't match that of DEPT table.
But if we change the projection to match the DEPT table then we have success again:
So, why can't we use a strongly-typed ref-cursor with dynamic SQL?
Because the compiler cannot parse the string in the dynamic SQL statement. So it cannot assert that the columns in the query's projection match in number and datatype the signature of the ref cursor. Consequently it cannot validate the contract between the ref cursor variable and the query. It is even easier to understand why this cannot be allowed when we consider that the dynamic SQL statement could be assembled from a query on USER_TAB_COLUMNS.
另一种可能性是声明并定义记录类型对象作为查询结果的容器。如果查询是 JOIN 查询,从多个连接表返回列,这可能很有用。
注意:您可以在动态构造的 SQL 查询语句上使用上述技术,方法是将“select deptno,ename,job from emp”替换为 v_sql 等变量,并使用过程主体中的 SQL 语句更新此变量。
Another possibility is to declare and define a Record Type object to be a container for your query results. This could be useful if the query is a JOIN query, returning columns from several joined tables.
NOTE: You could use the above technique on a dynamically constructed SQL query statement by substituting 'select deptno,ename,job from emp' with a variable such as v_sql and update this variable with the SQL statement within the body of the procedure.
不允许为动态sql打开强类型引用游标,因为oracle引擎无法检查动态sql的结构与引用游标的返回类型是否匹配。
You are not allowed to open a strongly typed ref cursor for a dynamic sql, as the oracle engine cannot check whether the structure of the dynamic sql is matching with that of the ref cursor's return type.