PL SQL 中多个参数的逗号分隔查询
Posted
技术标签:
【中文标题】PL SQL 中多个参数的逗号分隔查询【英文标题】:Comma Seperated Query for Multiple Parametes in PLSQL 【发布时间】:2015-10-15 14:58:23 【问题描述】:到目前为止,我可以参考Query with comma seperated IN parameters in PLSQL 在 PLSQL 中查询逗号分隔的 IN 参数。并且工作完美。我的问题是如何为至少 3 个逗号分隔的参数实现相同的解决方案。我的查询参数是这样的,
I_PRODUCT query (R%, L%)
I_MODEL query (E%,T%,R%)
I_TYPE query (A5,B%,C%)
作为一个函数并调用所有这些参数是否很好?还有其他快速解决方案吗?
create or replace PROCEDURE RQUERY1
(
I_PRODUCT VARCHAR2
I_MODEL VARCHAR2
I_TYPE VARCHAR2
, O_Cursor OUT SYS_REFCURSOR
) AS BEGIN
O_Cursor := NULL;
OPEN O_Cursor FOR
WITH PROD_SEARCH AS
(
select regexp_substr(I_PRODUCT,'[^,]+', 1, level) pattern from dual
connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null
)
SELECT * FROM table1
WHERE EXISTS (SELECT NULL FROM PROD_SEARCH WHERE table1.PRODUCT LIKE pattern );
END RQUERY1 ;
更新:我希望使用存储过程从我的 java 代码中查询参数(I_PRODUCT、I_MODEL、I_TYPE),并且需要显示输出值。
【问题讨论】:
【参考方案1】:使用集合:
CREATE OR REPLACE PROCEDURE RQUERY1
(
I_PRODUCT IN SYS.ODCIVARCHAR2LIST,
I_MODEL IN SYS.ODCIVARCHAR2LIST,
I_TYPE IN SYS.ODCIVARCHAR2LIST,
O_Cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN O_Cursor FOR
SELECT t.*
FROM table1 t
INNER JOIN TABLE( I_PRODUCT ) p ON t.PRODUCT = p.COLUMN_VALUE
INNER JOIN TABLE( I_MODEL ) m ON t.MODEL = m.COLUMN_VALUE
INNER JOIN TABLE( I_TYPE ) y ON t.TYPE = y.COLUMN_VALUE;
END RQUERY1;
/
然后你可以像这样在Java中调用它:
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.internal.OracleTypes;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
public class TestDatabase
public static void main(String args[])
try
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@url:port:sid","UserName","Password");
String[] products = "Product1", "Product2", "Product3" ;
String[] models = "Model1", "Model2", "Model3" ;
String[] types = "Type1", "Type2", "Type3" ;
ArrayDescriptor des = ArrayDescriptor.createDescriptor("SYS.ODCIVARCHAR2LIST", con);
CallableStatement st = con.prepareCall("call TEST.RQUERY1(?,?,?,?)");
st.setArray( 1, new ARRAY( des, con, products ) );
st.setArray( 2, new ARRAY( des, con, models ) );
st.setArray( 3, new ARRAY( des, con, types ) );
st.registerOutParameter( 4, OracleTypes.CURSOR );
st.execute();
ResultSet cursor = ((OracleCallableStatement)st).getCursor(4);
while ( cursor.next() )
int id = cursor.getInt(1);
String product = cursor.getString(2);
String model = cursor.getString(3);
String type = cursor.getString(4);
System.out.println( String.format( "Id: %5d", id ) );
System.out.println( String.format( " Product: %s\t", product ) );
System.out.println( String.format( " Model: %s", model ) );
System.out.println( String.format( " Type: %s", type ) );
catch(ClassNotFoundException | SQLException e)
System.out.println(e);
【讨论】:
【参考方案2】:编辑:
我终于明白你想要什么了。
如here 所述,您可以定义多个 with 语句。所以你可以这样写:
OPEN O_Cursor FOR
WITH PROD_SEARCH AS
(
select regexp_substr(I_PRODUCT,'[^,]+', 1, level) pattern from dual
connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null
),
MODEL_SEARCH AS
(
select regexp_substr(I_MODEL,'[^,]+', 1, level) pattern from dual
connect by regexp_substr(I_MODEL, '[^,]+', 1, level) is not null
),
TYPE_SEARCH AS
(
select regexp_substr(I_TYPE,'[^,]+', 1, level) pattern from dual
connect by regexp_substr(I_TYPE, '[^,]+', 1, level) is not null
)
SELECT * FROM table1
WHERE EXISTS (SELECT NULL FROM PROD_SEARCH WHERE table1.PRODUCT LIKE pattern )
AND EXISTS (SELECT NULL FROM MODEL_SEARCH WHERE table1.MODEL LIKE pattern );
AND EXISTS (SELECT NULL FROM TYPE_SEARCH WHERE table1.TYPE LIKE pattern );
END RQUERY1 ;
【讨论】:
更新了我的问题,如下所示:我希望使用存储过程从我的 java 代码中查询参数(I_PRODUCT、I_MODEL、I_TYPE),并需要显示输出值。 @KodS 我认为我明白你想要什么。编辑了答案。 谢谢,这正是我要找的以上是关于PL SQL 中多个参数的逗号分隔查询的主要内容,如果未能解决你的问题,请参考以下文章
如何在选择语句的“NOT IN”子句中使用逗号分隔的字符串列表作为 pl/sql 存储的函数参数