ORA-00932: 数据类型不一致: 预期 - 得到 -
作为一名 PHP 程序员,我使用 Oracle(10g.2) 已经快 3 年了,但是当我布置作业时,我第一次尝试使用引用游标和集合类型。而我 当我遇到问题时,我在网上进行了搜索,这个 ora-00932 错误确实让我不知所措。我需要一位老手的帮助。
这就是我一直在解决的问题 我想从表中选择行并将它们放入引用游标中,然后使用记录类型将它们收集在关联数组中。再次从这个关联数组中创建一个引用游标。不要问我为什么,我正在编写如此复杂的代码,因为我需要它来进行更复杂的作业。我可能会让你感到困惑,因此让我向你展示我的代码。
我在 Toad 的“类型”选项卡下定义了 2 种类型。其中一个是对象类型:
CREATE OR REPLACE
TYPE R_TYPE AS OBJECT(sqn number,firstname VARCHAR2(30), lastname VARCHAR2(30));
另一个是集合类型,它使用上面创建的对象类型:
CREATE OR REPLACE
TYPE tr_type AS TABLE OF r_type;
然后我创建一个包:
CREATE OR REPLACE PACKAGE MYPACK_PKG IS
TYPE MY_REF_CURSOR IS REF CURSOR;
PROCEDURE MY_PROC(r_cursor OUT MY_REF_CURSOR);
END MYPACK_PKG;
包主体:
CREATE OR REPLACE PACKAGE BODY MYPACK_PKG AS
PROCEDURE MY_PROC(r_cursor OUT MY_REF_CURSOR) AS
rcur MYPACK_PKG.MY_REF_CURSOR;
sql_stmt VARCHAR2(1000);
l_rarray tr_type := tr_type();
l_rec r_type;
BEGIN
sql_stmt := 'SELECT 1,e.first_name,e.last_name FROM hr.employees e ';
OPEN rcur FOR sql_stmt;
LOOP
fetch rcur into l_rec;
exit when rcur%notfound;
l_rarray := tr_type( l_rec );
END LOOP;
CLOSE rcur;
--OPEN r_cursor FOR SELECT * FROM TABLE(cast(l_rarray as tr_type) );
END MY_PROC;
END MYPACK_PKG;
我注释掉了打开引用光标的最后一行。因为当我在 Toad 的 SQL 编辑器中运行该过程时,它会导致另一个错误,这是我要问的第二个问题。 最后,我在 Toad 中运行代码:
variable r refcursor
declare
r_out MYPACK_PKG.MY_REF_CURSOR;
begin
MYPACK_PKG.MY_PROC(r_out);
:r := r_out;
end;
print :r
出现 ora-00932 错误。
I have been using Oracle(10g.2) as a PHP programmer for almost 3 years, but when I gave an assignment, I have tried to use the ref cursors and collection types for the first time. And I
've searched the web, when I faced with problems, and this ora-00932 error really overwhelmed me. I need help from an old hand.
Here is what I've been tackling with,
I want to select rows from a table and put them in a ref cursor, and then with using record type, gather them within an associative array. And again from this associative array, make a ref cursor. Don't ask me why, I am writing such a complicated code, because I need it for more complex assignment. I might be sound confusing to you, thus let me show you my codes.
I have 2 types defined under the types tab in Toad. One of them is an object type:
CREATE OR REPLACE
TYPE R_TYPE AS OBJECT(sqn number,firstname VARCHAR2(30), lastname VARCHAR2(30));
Other one is collection type which is using the object type created above:
CREATE OR REPLACE
TYPE tr_type AS TABLE OF r_type;
Then I create a package:
CREATE OR REPLACE PACKAGE MYPACK_PKG IS
TYPE MY_REF_CURSOR IS REF CURSOR;
PROCEDURE MY_PROC(r_cursor OUT MY_REF_CURSOR);
END MYPACK_PKG;
Package Body:
CREATE OR REPLACE PACKAGE BODY MYPACK_PKG AS
PROCEDURE MY_PROC(r_cursor OUT MY_REF_CURSOR) AS
rcur MYPACK_PKG.MY_REF_CURSOR;
sql_stmt VARCHAR2(1000);
l_rarray tr_type := tr_type();
l_rec r_type;
BEGIN
sql_stmt := 'SELECT 1,e.first_name,e.last_name FROM hr.employees e ';
OPEN rcur FOR sql_stmt;
LOOP
fetch rcur into l_rec;
exit when rcur%notfound;
l_rarray := tr_type( l_rec );
END LOOP;
CLOSE rcur;
--OPEN r_cursor FOR SELECT * FROM TABLE(cast(l_rarray as tr_type) );
END MY_PROC;
END MYPACK_PKG;
I commented out the last line where I open ref cursor. Because it's causing another error when I run the procedure in Toad's SQL Editor, and it is the second question that I will ask.
And lastly I run the code in Toad:
variable r refcursor
declare
r_out MYPACK_PKG.MY_REF_CURSOR;
begin
MYPACK_PKG.MY_PROC(r_out);
:r := r_out;
end;
print :r
There I get the ora-00932 error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用参考光标的方式并不常见。这将是使用它们的标准方法:
我不确定您要在这里完成什么,您要在过程中获取引用游标,然后返回另一个具有相同数据的引用游标。我认为在程序中根本没有必要获取光标。让调用应用程序执行获取操作(这里的获取操作是由
print
完成的)。更新:为什么您会收到无用的错误消息?
您正在使用动态打开的游标,我认为这是您收到无用错误消息的部分原因。如果我们使用固定 SQL,错误消息会有所不同:
我概述了当前在 10.2 中您可以将游标提取到 PLSQL 记录中,但不能在 SQL 对象中。
更新:关于
PLS-00306
:参数的数量或类型错误l_rarray 是一个嵌套表,它需要初始化然后扩展才能存储元素。例如:
要进一步阅读,您可以浏览 PL/SQL 集合的文档和记录。
The way you are using the REF CURSOR is uncommon. This would be the standard way of using them:
I'm not sure what you are trying to accomplish here, you're fetching the ref cursor inside the procedure and then returning another ref cursor that will have the same data. I don't think it's necessary to fetch the cursor at all in the procedure. Let the calling app do the fetching (here the fetching is done by the
print
).Update: why are you getting the unhelpful error message?
You're using a cursor opened dynamically and I think that's part of the reason you are getting the unhelpful error message. If we use fixed SQL the error message is different:
I outlined that currently in 10.2 you can fetch a cursor into a PLSQL record but not in a SQL Object.
Update: regarding the
PLS-00306
: wrong number or types of argumentsl_rarray is a NESTED TABLE, it needs to be initialized and then extended to be able to store elements. For example:
For further reading you can browse the documentation for PL/SQL collections and records.