I have a stored procedure that I want to update to include another condition in the WHERE clause based on an optional parameter. Right now I am checking my_id but I want to add a check against store_id but only if it's passed to the stored procedure (which is why I want to check if it's NOT NULL).
This is what I want:
CREATE OR REPLACE PROCEDURE MY_STORED_PROCEDURE (my_id IN VARCHAR2, store_id IN NUMBER DEFAULT null) AS
BEGIN
SELECT *
FROM MYTABLE
WHERE my_id = @my_id
//I want to add "AND" clause only if store_id is NOT NULL....something like:
//IF store_id IS NOT NULL
// AND store_id = @store_id
END;
I have tried to google search and also look around but haven't been able to find a way. I tried this but it's showing an error in SQL:
CREATE OR REPLACE EDITIONABLE PROCEDURE MY_STORED_PROCEDURE (my_id IN VARCHAR2, store_id IN NUMBER DEFAULT null) AS
BEGIN
SELECT *
FROM MYTABLE
WHERE my_id = @my_id
IF store_id IS NOT NULL
AND store_id = @store_id
END;
Example for clarification:
If I call the stored procedure MY_STORED_PROCEDURE(1234), then the query should be:
SELECT *
FROM MYTABLE
WHERE my_id = 1234
If I call the stored procedure MY_STORED_PROCEDURE(1234, 987) then the query should be:
SELECT *
FROM MYTABLE
WHERE my_id = 1234 AND store_id = 987
You may use the following logic: