选择Object Table Into Variable

时间:2015-09-23 23:38:44

标签: sql oracle object plsql

我在尝试从对象表中选择一些东西到我自己的局部变量时遇到了问题。这是一些基本代码。

create type my_obj as object
(
  my_number number
)
/
create table my_table of my_obj;
/
insert into my_table values (my_obj(123))
/
declare
  my_holder my_obj;
begin
  select * into my_holder from my_table where rownum = 1;
  dbms_output.put_line(my_holder.my_number);
end;

我离开的错误是

Error starting at line : 86 in command -
declare
  my_holder my_obj;
begin
  select * into my_holder from my_table where rownum = 1;
end;
Error report -
ORA-06550: line 4, column 10:
PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got NUMBER
ORA-06550: line 4, column 3:
PL/SQL: SQL Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

有关为什么会失败的任何线索?

1 个答案:

答案 0 :(得分:3)

select返回对象中的变量而不是对象本身,所以如果你想让它以你拥有它的方式工作,你需要声明你想要返回的对象如下所示。

select my_obj(my_number) into my_holder from my_table where rownum = 1;

希望这有帮助