不一致的数据类型:在包函数中从游标返回表时 - ORACLE

时间:2016-05-18 01:10:00

标签: sql oracle function package

我有以下包裹。我试图从游标填充函数内的记录并返回记录。我不确定如何将游标中的行分配到记录变量中。我需要返回记录,以便我可以从中创建物化视图。

CREATE OR REPLACE PACKAGE pkg_contrator_of_consultant AS

    TYPE cst_record IS RECORD(
       consultant_id NUMBER(10));

    TYPE cst_id_type IS TABLE OF cst_record;

    FUNCTION fnc_get_contractor_id(cst_username IN VARCHAR2)
        RETURN cst_id_type
        PIPELINED;
END;


CREATE OR REPLACE PACKAGE BODY pkg_contrator_of_consultant AS 

    FUNCTION fnc_get_contractor_id(cst_username IN VARCHAR2 )
        RETURN cst_id_type
        PIPELINED IS    

        V_cursor_contracotr_id cst_id_type;

        CURSOR cursor_contract_name IS
        SELECT plc.FK2_CONTRACTOR_ID
            FROM lds_consultant cons
            INNER JOIN lds_account acc on cons.consultant_id = acc.fk1_consultant_id 
            INNER JOIN lds_placement plc on acc.account_id = plc.FK1_ACCOUNT_ID
            WHERE UPPER(cons.USERNAME) = UPPER(cst_username)
            AND UPPER(plc.PLT_TO_PERMANENT) = UPPER('Y');

            V_contracotr_id cst_id_type;
        BEGIN

            FOR rec IN cursor_contract_name LOOP

                V_contracotr_id := rec.fk2_contractor_id;

                SELECT V_contracotr_id INTO V_cursor_contracotr_id FROM DUAL;

                dbms_output.put_line('cst_username : '||cst_username||'  V_contracotr_id :'||V_contracotr_id);


            END LOOP;

            PIPE ROW (V_cursor_contracotr_id);
            RETURN;
        END fnc_get_contractor_id;
    END;
    /

在第

V_contracotr_id := rec.fk2_contractor_id;

当光标选择的列为NUMBER类型时,它会给出错误“不一致的数据类型:预期的UDT得到NUMBER”。

FK2_CONTRACTOR_ID   NUMBER

1 个答案:

答案 0 :(得分:2)

试试这个:

        out_rec cst_record;

        CURSOR C1 IS
        SELECT ...;

  BEGIN

    open c1;
    LOOP
    FETCH c1 INTO out_rec;

  exit when c1%notfound;

    PIPE ROW(out_rec);

  END LOOP;

  close c1;

  RETURN;

END fnc_get_contractor_id;

更新代码:

CREATE OR REPLACE PACKAGE pkg_contrator_of_consultant AS

    TYPE cst_record IS RECORD(
       consultant_id NUMBER(10));

    TYPE cst_id_type IS TABLE OF cst_record;

    FUNCTION fnc_get_contractor_id(cst_username IN VARCHAR2)
        RETURN cst_id_type
        PIPELINED;
END;
/

CREATE OR REPLACE PACKAGE BODY pkg_contrator_of_consultant AS 
FUNCTION fnc_get_contractor_id(cst_username IN VARCHAR2 )
    RETURN cst_id_type
    PIPELINED IS    

    CURSOR c1 IS
    SELECT plc.FK2_CONTRACTOR_ID
        FROM lds_consultant cons
        INNER JOIN lds_account acc on cons.consultant_id = acc.fk1_consultant_id 
        INNER JOIN lds_placement plc on acc.account_id = plc.FK1_ACCOUNT_ID
        WHERE UPPER(cons.USERNAME) = UPPER(cst_username)
        AND UPPER(plc.PLT_TO_PERMANENT) = UPPER('Y');

        out_rec cst_record;
    BEGIN

        open c1;
        LOOP
        FETCH c1 INTO out_rec;

      exit when c1%notfound;

        PIPE ROW(out_rec);

      END LOOP;

      close c1;

      RETURN;

    END fnc_get_contractor_id;
END;
/