inconsistent datatypes: when returning table from

2019-07-23 13:04发布

I have the following package. I am trying to fill the records inside the function from a cursor and return the record. I am unsure of how to assign the rows from the cursor into the record variable. I need to return the record so that I could create a materialized view from it.

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;
    /

In the line

V_contracotr_id := rec.fk2_contractor_id;

It gives the error "inconsistent datatypes: expected UDT got NUMBER" when the column selected by the cursor is of NUMBER type.

FK2_CONTRACTOR_ID   NUMBER

1条回答
趁早两清
2楼-- · 2019-07-23 13:08

Try this:

        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;

UPDATED CODE:

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;
/
查看更多
登录 后发表回答