How to enter binds for a multi-valued parameter in SQL Developer

前端 未结 1 1449
天涯浪人
天涯浪人 2021-01-19 05:04

I have a lot of SQL with named parameters that I need to be able to execute in SQL Developer. For SQL where the parameters are scalar values it\'s easy to paste the SQL into

相关标签:
1条回答
  • 2021-01-19 05:50

    This isn't a SQL Developer restriction, it's just how bind variables work. You're effectively doing:

    select count(*) from foo 
    where foo.id in ('1,2,3')
    

    ... which is really in (to_number('1,2,3')), hence the error. It'll work for a single value, give odd results for two values if your decimal separator is a comma, and fail for anything more.

    You can't enter multiple values at a bind prompt, or supply multiple values to an in() with a single bind. You can cheat be a bit inventive though. The xmltable function will convert the comma-separated string into rows with one value in each:

    var ids varchar2(50);
    exec :ids := '1,2,3';
    select * from xmltable(:ids);
    
    COLUMN_VALUE
    ------------
    1            
    2            
    3            
    

    You can then use that as a look-up table:

    select count(*)
    from xmltable(:ids) x
    join foo f on f.id = to_number(x.column_value);
    
      COUNT(*)
    ----------
             3 
    
    0 讨论(0)
提交回复
热议问题