0

I have a SQL statement as shown below. In that i wanted to have IN clause values like 'X','Y','Z' and this value may vary based on different conditions. But when i give this value at run time SQL statement not giving me any output.

SELECT test,test1,test2
FROM test_n 
WHERE NVL(code,'X') in(:code);

here

:code = 'X','Y','Z'

Can any one help me?

Also i cannot use anonymous block or functions to achieve this.

Kamil Gosciminski
  • 16,547
  • 8
  • 49
  • 72
teepu
  • 266
  • 1
  • 6
  • 18

2 Answers2

0

You could use DEFINE by quoting the string ( substitution variables)

SET DEFINE ':'
DEFINE code  = "'X','Y','Z'"

SELECT test,test1,test2
FROM test_n 
WHERE NVL(code,'X') in(:code);

If you want to use true bind variables, you could try something like this with a LIKE statement.

set define off
VARIABLE code VARCHAR2
EXEC :code := 'X,Y,Z'
select test,test1,test2
from   test_n
where  ',' || :code || ',' like '%,' || code || ',%'
;
Kaushik Nayak
  • 30,772
  • 5
  • 32
  • 45
0

You could use something like :

with tab as ( SELECT REGEXP_SUBSTR ('X,Y,Z', '[^,]+', 1, LEVEL) code FROM DUAL CONNECT BY REGEXP_SUBSTR ('X,Y,Z','[^,]+',1,LEVEL) IS NOT NULL) select test,test1,test2 from test_n where nvl(code,'X') in (select code from tab)

vishad
  • 1,134
  • 7
  • 11