using comma separated values inside IN clause for NUMBER column

后端 未结 4 1164
情话喂你
情话喂你 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 00:47

    The problem is that oracle does not interprete the VARCHAR2 string you're passing as a sequence of numbers, it is just a string.

    A solution is to make the whole query a string (VARCHAR2) and then execute it so the engine knows he has to translate the content:

    DECLARE
        TYPE T_UT IS TABLE OF users_Table%ROWTYPE;
        aVar T_UT;
    BEGIN
        EXECUTE IMMEDIATE 'select * from users_Table where user_id in (' || l_userIds || ')' INTO aVar;
    ...
    
    END;
    

    A more complex but also elegant solution would be to split the string into a table TYPE and use it casted directly into the query. See what Tom thinks about it.

提交回复
热议问题