using comma separated values inside IN clause for NUMBER column

后端 未结 4 1160
情话喂你
情话喂你 2020-12-20 00:21

I have 2 procedures inside a package. I am calling one procedure to get a comma separated list of user ids.

I am storing the result in a VARCHAR variab

4条回答
  •  一整个雨季
    2020-12-20 01:01

    Do you really need to return a comma-separated list? It would generally be much better to declare a collection type

    CREATE TYPE num_table
        AS TABLE OF NUMBER;
    

    Declare a function that returns an instance of this collection

    CREATE OR REPLACE FUNCTION get_nums
      RETURN num_table
    IS
      l_nums num_table := num_table();
    BEGIN
      for i in 1 .. 10
      loop
        l_nums.extend;
        l_nums(i) := i*2;
      end loop;
    END;
    

    and then use that collection in your query

    SELECT *
      FROM users_table
     WHERE user_id IN (SELECT * FROM TABLE( l_nums ));
    

    It is possible to use dynamic SQL as well (which @Sebas demonstrates). The downside to that, however, is that every call to the procedure will generate a new SQL statement that needs to be parsed again before it is executed. It also puts pressure on the library cache which can cause Oracle to purge lots of other reusable SQL statements which can create lots of other performance problems.

提交回复
热议问题