Unable to pass string value 1,2 as input to an oracle query [duplicate]

对着背影说爱祢 提交于 2019-11-28 14:24:59

You can use string comparisons instead of an IN condition.

select ...
from   ...
where  ',' || :emp_id || ',' like '%,' || to_char(emp_id) || ',%'
;

In is used with list of values or subqueries.

You can convert a comma separeted string into a subquery, by using the following hack:

 SELECT TRIM(REGEXP_SUBSTR(temp, '[^,]+', 1, level))   
    FROM (SELECT  :emp_id temp FROM DUAL)
    CONNECT BY level <= REGEXP_COUNT(temp, '[^,]+')

Here the 1,2,3 sting will be converted into subquery that returns 3 rows.

So, the end result, for your case, could be something like this:

SELECT  e.*
FROM    employee_detail e
WHERE   e.emp_id in (
SELECT decode(:emp_id,null,  (select  e.emp_id from dual) 
,TRIM(REGEXP_SUBSTR(temp, '[^,]+', 1, level)) )  
    FROM (SELECT  :emp_id temp FROM DUAL)
    CONNECT BY level <= REGEXP_COUNT(temp, '[^,]+'))

Note that in this case In will return true if :emp_id is null and this was deliberatly achieved by using decode function.

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