I have a SELECT
query in which i will have a a Dynamic WHERE
condition. The thing is when I try to concatenate the WHERE
condition PARAMETER
with the SQL Query its not allowing me to save the PROCEDURE
.
eg:
CREATE PROCEDURE usp_MySearchQuery
(
QTYPE IN INT,
OUT_CUR OUT SYS_REFCURSOR
)
IS
DYN_QUERY VARCHAR2;
BEGIN
IF QTYPE=1 THEN
DYN_QUERY :=' BETWEEN 1 AND 2';
ELSE
DYN_QUERY :='=10';
END IF;
OPEN OUT_CUR FOR
SELECT * FROM MYTABLE WHERE TYPE=QTYPE AND ID || DYN_QUERY;
END;
This is how my procedure looks like.
I tried EXECUTE IMMEDIETE
but in its documentation itself, its written it wont work for multiple row query.
In MSSQL
we have EXEC
(not sure) command which can execute
the text sent to the command. In the same way do we have any commands which can run the dynamic query in Oracle
UPDATE: Answer
I tried like this.
OPEN OUT_CUR FOR
' SELECT * FROM MYTABLE WHERE TYPE=:QTYPE AND ID ' || DYN_QUERY
USING QTYPE;
and it worked