3

I have a simple select query:

SELECT A
FROM B
WHERE A IN :conditions

where A is a char.

If i do the full query as:

SELECT A
FROM B
WHERE A IN ('A','E','I','O','U')

it works

but if i use the parameter, when i try to execute with ORACLE SQL Developer i cannot get it to work.

I've already tried to submit the parameter as:

1* ('A','E','I','O','U')
2* 'A','E','I','O','U'
3* A,E,I,O,U

and query returns no results

What's the correct syntax to do so?

Thank U all

0

3 Answers 3

2

You may use a substitution variable in SQL Developer (or in SQL* Plus). Use double quotes while defining.

Test data

create table b as select 'A' as a from dual union all 
select 'I' as a from dual union all
select 'U' as a from dual

Execution

define params = "'A','E','I','O','U'"
select A from B where A in (&params);

Result

old:select A from B where A in (&params)
new:select A from B where A in ('A','E','I','O','U')

A
-
A
I
U
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Kaushik, Thanks for your answer, but I just want to submit the parameter in the SQL Developer UI. I don't want to change the query.
@TiagoDias : Nowhere did I ask you to "change" your query. I told you to simply use a double quoted substitution parameter ( either by defining it or passing as user input )
I've been looking for a way to easily use variables in select statements in PL/SQL for years. This is fantastic.
1

Passing comma separated string as bind variable for VO query's IN operator

This problem vexed me for years before I discovered this solution.

I used this technique to build an object search at the CLI, where I pass in a list of schemas to a :BIND

SELECT owner,
       object_name,
       object_type
  FROM all_objects
 WHERE object_name LIKE :SEARCH
   AND owner NOT IN (
    'SYS',
    'MDSYS',
    'DBSNMP',
    'SYSTEM',
    'DVSYS',
    'APEX_050100',
    'PUBLIC',
    'ORDS_METADATA',
    'APEX_LISTENER'
)
   AND object_type IN (
    SELECT regexp_substr(:bind_ename_comma_sep_list,'[^,]+',1,level)
      FROM dual CONNECT BY
        regexp_substr(:bind_ename_comma_sep_list,'[^,]+',1,level) IS NOT NULL
)
 ORDER BY owner,
          object_name,
          object_type;

2 Comments

Hi. Thanks for your answer, but I just want to submit the parameter in the SQL Developer UI. I don't want to change the query.
The UI includes an editor...where you write SQL...if you want to do this, you need to change your SQL.
0

I Found a way to do it for Oracle based on this post: https://sqlstudies.com/2013/04/08/how-do-i-use-a-variable-in-an-in-clause/

Would be something like this:

SELECT A
FROM B
WHERE ',' || :conditions || ',' LIKE '%,' || A || ',%'

Probably it is not the best performance option but solves the issue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.