ORA-00947:函数返回记录表的值不足

时间:2019-06-05 17:40:43

标签: oracle function bulk-collect

因此,我正在尝试构建一个函数,该函数返回某些客户端订阅中包含的项目的记录。

所以我一直在建立以下内容:

2种类型:

CREATE OR REPLACE TYPE PGM_ROW AS OBJECT
    (
        pID      NUMBER(10),
        pName    VARCHAR2(300)
    );

CREATE OR REPLACE TYPE PGM_TAB AS TABLE OF PGM_ROW;

1个功能:

CREATE OR REPLACE FUNCTION FLOGIN (USER_ID NUMBER) RETURN PGM_TAB
AS  
    SELECTED_PGM   PGM_TAB;
BEGIN
    FOR RESTRICTION
        IN (  SELECT (SELECT LISTAGG (ID_CHANNEL, ',')
                                 WITHIN GROUP (ORDER BY ID_CHANNEL)
                        FROM (SELECT DISTINCT CHA2.ID_CHANNEL
                                FROM CHANNELS_ACCESSES CHA2
                                     JOIN CHANNELS CH2
                                         ON CH2.ID = CHA2.ID_CHANNEL
                               WHERE     CHA2.ID_ACCESS = CMPA.ID_ACCESS
                                     AND CH2.ID_CHANNELS_GROUP = CG.ID))
                         AS channels,
                     (SELECT LISTAGG (ID_SUBGENRE, ',')
                                 WITHIN GROUP (ORDER BY ID_SUBGENRE)
                        FROM (SELECT DISTINCT SGA2.ID_SUBGENRE
                                FROM SUBGENRES_ACCESSES SGA2
                                     JOIN CHANNELS_ACCESSES CHA2
                                         ON CHA2.ID_ACCESS = SGA2.ID_ACCESS
                                     JOIN CHANNELS CH2
                                         ON CH2.ID = CHA2.ID_CHANNEL
                               WHERE     SGA2.ID_ACCESS = CMPA.ID_ACCESS
                                     AND CH2.ID_CHANNELS_GROUP = CG.ID))
                         AS subgenres,
                     CG.NAME,
                     A.BEGIN_DATE,
                     A.END_DATE,
                     CMP.PREVIEW_ACCESS
                FROM USERS U
                     JOIN COMPANIES_ACCESSES CMPA
                         ON U.ID_COMPANY = CMPA.ID_COMPANY
                     JOIN COMPANIES CMP ON CMP.ID = CMPA.ID_COMPANY
                     JOIN ACCESSES A ON A.ID = CMPA.ID_ACCESS
                     JOIN CHANNELS_ACCESSES CHA
                         ON CHA.ID_ACCESS = CMPA.ID_ACCESS
                     JOIN SUBGENRES_ACCESSES SGA
                         ON SGA.ID_ACCESS = CMPA.ID_ACCESS
                     JOIN CHANNELS CH ON CH.ID = CHA.ID_CHANNEL
                     JOIN CHANNELS_GROUPS CG ON CG.ID = CH.ID_CHANNELS_GROUP
               WHERE U.ID = USER_ID
            GROUP BY CG.NAME,
                     A.BEGIN_DATE,
                     A.END_DATE,
                     CMPA.ID_ACCESS,
                     CG.ID,
                     CMP.PREVIEW_ACCESS)
    LOOP
        SELECT PFT.ID_PROGRAM, PFT.LOCAL_TITLE
          BULK COLLECT INTO SELECTED_PGM
          FROM PROGRAMS_FT PFT
         WHERE     PFT.ID_CHANNEL IN
                       (    SELECT TO_NUMBER (
                                       REGEXP_SUBSTR (RESTRICTION.CHANNELS,
                                                      '[^,]+',
                                                      1,
                                                      ROWNUM))
                              FROM DUAL
                        CONNECT BY LEVEL <=
                                   TO_NUMBER (
                                       REGEXP_COUNT (RESTRICTION.CHANNELS,
                                                     '[^,]+')))
               AND PFT.ID_SUBGENRE IN
                       (    SELECT TO_NUMBER (
                                       REGEXP_SUBSTR (RESTRICTION.SUBGENRES,
                                                      '[^,]+',
                                                      1,
                                                      ROWNUM))
                              FROM DUAL
                        CONNECT BY LEVEL <=
                                   TO_NUMBER (
                                       REGEXP_COUNT (RESTRICTION.SUBGENRES,
                                                     '[^,]+')))
               AND (PFT.LAUNCH_DATE BETWEEN RESTRICTION.BEGIN_DATE
                                        AND RESTRICTION.END_DATE);
    END LOOP;
    RETURN SELECTED_PGM;
END FLOGIN;

我希望函数tu返回一个包含2列的表,其中包含用户访问权限中包含的表PROGRAMS_FT中的所有记录。

由于某种原因,我收到编译警告ORA-000947。 我对错误代码的理解是,当插入的值与接收值的对象的类型不匹配时会发生此错误,并且在这里我看不到这种情况。

1 个答案:

答案 0 :(得分:0)

您正在选择两个标量值,然后尝试将它们放入对象中。这不会自动发生,您需要将它们转换为对象:

...
    LOOP
        SELECT PGM_ROW(PFT.ID_PROGRAM, PFT.LOCAL_TITLE)
          BULK COLLECT INTO SELECTED_PGM
          FROM PROGRAMS_FT PFT
...

(这是PL / SQL的一个无用的怪癖,它说“没有足够的值”而不是“太多的值”,正如您在尝试将两件事合为一体时可能期望的那样;我敢肯定,我想出了一次令人信服的解释/辩解,但此刻我不知道...)

我不确定您的循环是否有意义。假设您的游标查询返回多行,每次循环时您都将SELECTED_PGM集合的内容替换-您可能会认为您要追加它,但这不是它的工作方式。因此,您最终将仅根据循环的最终迭代返回集合。

聚合然后拆分数据似乎也需要很多工作。您可能会为这些使用集合;但是您可能可以摆脱游标和循环的局限,并将游标查询与内部查询结合起来,这样会更高效,并且可以对所有组合数据进行单个批量收集。