Reputation: 377
I wrote the following in Oracle 11g to separate IN params (I_PRODUCT)and make a query. When I give one parameter as the i_PRODUCT, it is populating some results. When I am inserting multiple comma separated parameters, I am expecting to query one by one and populate the entire result and it is not working.
create or replace PROCEDURE RQUERY
(
I_PRODUCT VARCHAR2
, O_Cursor OUT SYS_REFCURSOR
) AS BEGIN
O_Cursor := NULL;
OPEN O_Cursor for
SELECT * FROM Table1 WHERE
Table1.PRODUCT LIKE ( select regexp_substr(I_PRODUCT,'[^,]+', 1, level) from dual
connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null);
END RQUERY ;
Upvotes: 1
Views: 202
Reputation: 182
create or replace PROCEDURE ProcedureName
(
I_PRODUCT IN VARCHAR2 :=NULL,
O_Cursor OUT SYS_REFCURSOR
)
AS
BEGIN
DECLARE I_PRODUCT_ VARCHAR2(1000);
BEGIN
I_PRODUCT_ := ',' || nvl(I_PRODUCT,'') || ',';
Open O_Cursor FOR
SELECT * FROM Table1 WHERE 1=1
And (I_PRODUCT_ = ',,' Or I_PRODUCT_ Like '%,' || I_PRODUCT ||',%');
END;
END ProcedureName;
Upvotes: 1
Reputation: 49082
SELECT * FROM Table1 WHERE
Table1.PRODUCT LIKE ( select regexp_substr(I_PRODUCT,'[^,]+', 1, level) from dual
connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null);
The LIKE operator would fail in above query. You need to use IN, which would internally be evaluated as multiple OR.
Table1.PRODUCT IN ( select regexp_substr(I_PRODUCT,'[^,]+', 1, level) from dual
connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null);
Also, regarding varying IN list, have a look at my answer here.
Update
Based on OP's comments.
Modify the query as:
WITH str_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 str_search
WHERE tab.col LIKE pattern
);
Upvotes: 1
Reputation: 9345
use IN
instead of like
;
Table1.PRODUCT IN(
select regexp_substr(I_PRODUCT,'[^,]+', 1, level) from dual
connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null
)
Upvotes: 0