通过函数从表中检索行

时间:2016-06-24 14:05:37

标签: oracle function

我想要做什么:使用自定义功能并使用自定义类型从表中检索数据。

我桌子的结构如下:

TypeLoadException

我做了什么:创建自定义类型

create table ACCOUNTCONTRACT
(
  accountcode_            VARCHAR2(255 CHAR),
  accountmig_             NUMBER(1),
  accountnumber_          VARCHAR2(25 CHAR) not null,
  accountpk_              NUMBER(19),
  addinfo1_               VARCHAR2(255 CHAR),
  addinfo2_               VARCHAR2(255 CHAR),
...

CREATE OR REPLACE TYPE "FO_OUTPUT" AS OBJECT
(
  NUMCPT        VARCHAR2(10),
  ACTIONACCOUNT VARCHAR2(50)
)

我的职能是:

CREATE OR REPLACE TYPE "FO_OUTPUT_TABLE" AS TABLE OF FO_OUTPUT

现在当我执行时:`SELECT * FROM TABLE(OUTPUT_FO)我得到的是10个空行。

我的代码出了什么问题。 Thnx

3 个答案:

答案 0 :(得分:1)

CREATE OR REPLACE FUNCTION OUTPUT_FO RETURN FO_OUTPUT_TABLE AS RETVAL FO_OUTPUT_TABLE := FO_OUTPUT_TABLE();
TMPLINE FO_OUTPUT;
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
  TMPLINE := FO_OUTPUT('', ''); 
  FOR I IN (SELECT SUBSTR(ACC.ACCOUNTNUMBER_, 1, 10) AS NUMCPT,
                   ACC.ACCOUNTNUMBER_ AS ACTIONACCOUNT
              --INTO TMPLINE.NUMCPT, TMPLINE.ACTIONACCOUNT << this does not work
              FROM ACCOUNTCONTRACT ACC
             WHERE ROWNUM < 10) LOOP

    -- But this:                 
    TMPLINE.numcpt:= i.numcpt;
    TMPLINE.ACTIONACCOUNT:= i.ACTIONACCOUNT;

    RETVAL.EXTEND;
    RETVAL(RETVAL.COUNT) := TMPLINE;
  END LOOP;
  RETURN(RETVAL);
END OUTPUT_FO;

答案 1 :(得分:1)

使用BULK COLLECT INTO

CREATE OR REPLACE FUNCTION OUTPUT_FO
RETURN FO_OUTPUT_TABLE
AS
  RETVAL  FO_OUTPUT_TABLE;
BEGIN
  SELECT FO_OUTPUT(
           SUBSTR(ACC.ACCOUNTNUMBER_, 1, 10),
           ACC.ACCOUNTNUMBER_
         )
  BULK COLLECT INTO RETVAL
  FROM   ACCOUNTCONTRACT ACC
  WHERE  ROWNUM < 10;

  RETURN RETVAL;
END OUTPUT_FO;
/

答案 2 :(得分:0)

我在这里是如何让它发挥作用的:

CREATE OR REPLACE FUNCTION OUTPUT_FO RETURN FO_OUTPUT_TABLE AS
  RETVAL  FO_OUTPUT_TABLE := FO_OUTPUT_TABLE();
BEGIN
  FOR I IN (SELECT SUBSTR(ACC.ACCOUNTNUMBER_, 1, 10) AS NUMCPT,
                   ACC.ACCOUNTNUMBER_ AS ACTIONACCOUNT
              FROM ACCOUNTCONTRACT ACC
             WHERE ROWNUM < 10) LOOP

    RETVAL.EXTEND;
    RETVAL(RETVAL.COUNT) := FO_OUTPUT(I.NUMCPT, I.ACTIONACCOUNT);
  END LOOP;
  RETURN(RETVAL);
END OUTPUT_FO;