1

The below is the snippet where I pass a cursor as input paramter in Oracle. And Im trying to convert the same to EntepriseDB.

create or replace FUNCTION FUNCTION_1
                                                       (
                                                           p_source IN SYS_REFCURSOR
                                                       )
    is
    TYPE row_ntt
    IS
        TABLE OF VARCHAR2(32767);
        v_rows row_ntt;
    begin
       ---            
        LOOP
            FETCH p_source BULK COLLECT
            INTO  v_rows LIMIT 1000
            ;

            FOR i IN 1 .. v_rows.COUNT
            LOOP
                -- Will do something here
            END LOOP;
            EXIT    WHEN p_source%NOTFOUND;
        END LOOP;
    END;

it's called in the below way in Oracle:

SELECT
  *
FROM
  TABLE ( function_1(CURSOR(select col1 from tbl)));

Now after migration

I changed like below

create or replace FUNCTION FUNCTION_1
                                                       (
                                                           p_source IN SYS_REFCURSOR
                                                       )
    is
    TYPE row_ntt
    IS
        TABLE OF VARCHAR2(32767);
        v_rows row_ntt;
    begin
       ---            
       select unnest(p_source) into v_rows; 

            FOR i IN 1 .. v_rows.COUNT
            LOOP
                -- Will do something here
            END LOOP;

    END;

Im calling in the below way in EntepriseDB/Postgresql

 SELECT
      *
    FROM
      TABLE (function_1(ARRAY(
          SELECT
             col1 from table)));

Im getting below error at assigning the value to v_rows.

ERROR:  cannot assign non-table type to a table

Please help me in making unnest as table type so that it inserts into v_rows. or else if there is a way I can use sysrefcursor in EnterpriseDB is also fine. Please let me know how to call in ENtepriseDB when sysrefcursor is used. Thanks!!

2
  • I don't know what EnterpriseDB gives you, but both functions you show are Oracle functions. I don't see what has been migrated, except that unnest function is used in the second. Anyway, If you want to run Oracle's select from TABLE() like query in Postgres, here's an example you may refer. Apr 20, 2018 at 15:39
  • @KaushikNayak I got the answer by changing v_rows as v_rows text[] and make unnest as array like select array from (Select unnest(...)), basically I made table type as array. by the way, you missed adding the reference link. Apr 23, 2018 at 6:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.