简体   繁体   English

如何使用参数plsql将条件IN列表转换为字符串oracle?

[英]how to convert condition IN list into string oracle using parameter plsql?

I need to convert a condition IN list to a list of string using parameter. 我需要使用参数将条件输入列表转换为字符串列表。

from: 从:

:p_parameter in ('a','b','c') :p_parameter在('a','b','c')中

to: 至:

:p_parameter = (a,b,c) :p_parameter =(a,b,c)

For example select kod from dual where kod in(:p_parameter) 例如,从双中选择kod,其中kod in(:p_parameter)

if the input is :p_parameter in (a,b,c) then the output show 如果输入是(a,b,c)中的:p_parameter,则输出显示

kod
------
a
b
c

Anyone got the idea? 有人知道吗?

Maybe LISTAGG will help you 也许LISTAGG会帮助你

    SELECT col1
           LISTAGG(p, ',') WITHIN GROUP (ORDER BY p) AS p
    FROM   p_tgable
    WHERE  p in ('a','b','c')
    GROUP  BY
           col1;

Can be done with Nested Table collection type. 可以使用嵌套表集合类型来完成。

First declare a custom nested type, let's name it STRINGS_TYPE: 首先声明一个自定义的嵌套类型,我们将其命名为STRINGS_TYPE:
create type STRINGS_TYPE AS TABLE OF VARCHAR2(100);

Then you can compare collections for equality, for example this will return 1 because these two collections are equal: 然后,您可以比较集合是否相等,例如,这将返回1,因为这两个集合相等:

select 1 from dual 
where STRINGS_TYPE('a', 'b' , 'c' ) = STRINGS_TYPE('a', 'b', 'c')

Order does not matter, collections are equal, returns 1: 顺序无所谓,集合相等,返回1:

select 1 from dual 
where STRINGS_TYPE( 'b','a',  'c' ) = STRINGS_TYPE('a', 'b', 'c')

These two collections are not equal, nothing is returned: 这两个集合不相等,什么也不返回:

select 1 from dual 
where STRINGS_TYPE( 'x','y', 'z', 'a', 'b', 'c' ) = STRINGS_TYPE('a', 'b', 'c')

In your code bind :p_parameter parameter like this: 在您的代码中,像这样绑定:p_parameter参数:

String[] pArray = new String[]{"a", "b", "c"};

String selectSQL = "select 1 from dual where  :p_parameter = STRINGS_TYPE('a', 'b', 'c')";

Connection connection = database.getConnection();  

oracle.jdbc.OraclePreparedStatement preparedStatement = (oracle.jdbc.OraclePreparedStatement )connection.prepareStatement(selectSQL);

oracle.sql.ArrayDescriptor descriptor =  oracle.sql.ArrayDescriptor.createDescriptor("YOUR_SCHEMA.STRINGS_TYPE", connection);
oracle.sql.ARRAY pOracleARRAY = new oracle.sql.ARRAY(descriptor, connection, pArray);
preparedStatement.setARRAY(1, pOracleARRAY);

resultSet = preparedStatement.executeQuery();
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM