Pass comma separated number to IN clause in Stored Procedure

梦想与她 提交于 2019-12-12 04:53:33

问题


I have a stored procedure which takes a varchar parameter called P_LOCATIONS in Oracle. This locations parameter has location id's that are comma separated. In database locationId is a number.

Following sql query throws an Invalid number when there are more than one locations. I understand that because of comma its not able to convert a varchar into a number. How can I achieve this?

    create or replace PROCEDURE       GET_RAW_DATA
      (   P_LOCATIONS IN VARCHAR2, 
          Results   OUT   SYS_REFCURSOR
        )AS 
        BEGIN

       select * from tableName where LOCATION_ID IN ( P_LOCATIONS);
      END GET_RAW_DATA;

回答1:


The end result of what you are doing is this:

select * from tableName where LOCATION_ID IN ('1,2,3');

And what you need is this:

select * from tableName where LOCATION_ID IN (1,2,3);

So you can use this:

select * from tableName where LOCATION_ID in (
    select regexp_substr(P_LOCATIONS,'[^,]+{1}',1,level)
    from dual connect by level <= length(regexp_replace(P_LOCATIONS,'[^,]*')) + 1
);


来源:https://stackoverflow.com/questions/24540337/pass-comma-separated-number-to-in-clause-in-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!