到目前为止,我可以参考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),并且需要显示输出值。
答案 0 :(得分:2)
使用集合:
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);
}
}
}
答案 1 :(得分:0)
修改强>
我终于理解了你想要的东西。
如上所述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 ;