将Oracle游标行转换为流水线函数的用户定义记录类型

时间:2012-08-21 12:08:21

标签: oracle types casting plsql cursor

在我的包中,我定义了record类型和相应的table类型。然后我有一个流水线函数打开一个游标,并试图管出每个结果。问题是它给了我类型不匹配错误。我试图将光标转换为我的记录类型,但无济于事:我做错了什么?

create or replace package myTest as
  type myRec is record(
    id  integer,
    foo varchar2(10)
  );
  type myTable is table of myRec;

  function output() return myTable pipelined;
end myTest;
/

create or replace package body myTest as
  function output() return myTable pipelined
  as
  begin
    for myCur in (
      select id, foo from someTable
    )
    loop
      pipe row(cast(myCur as myRec));
    end loop;

    return;
  end output;
end myTest;
/

1 个答案:

答案 0 :(得分:4)

create or replace package body myTest as
  function output return myTable pipelined
  as
    --   Add a "record" variable":
    xyz  myRec;
  begin
    for myCur in (
      select id, foo from someTable
    )
    loop
      -- Fill the record variable with the
      -- values from the cursor ...
      xyz.id  := myCur.id;
      xyz.foo := myCur.foo;
      -- ... and pipe it:
      pipe row(xyz);
    end loop;

    return;
  end output;
end myTest;
相关问题