I’m writing stored procedure that should search for records base on few procedure arguments.
The problem is that not always all arguments have to be passed, sometimes they may be set to NULL.
Is there a way to write sth that would work like that?
CREATE PROCEDURE testProc(IN p_idWorker INTEGER, IN p_idEffect INTEGER)
BEGIN
SELECT
*
FROM
CallHistory
WHERE
idWorker = IFNULL(p_idWorker, ANYTHING)
AND
idEffect = IFNULL(p_idEffect, ANYTHING);
END$$
First of all thank you Mahmoud and valex for your time but both answers are not totally good. They will not work if for example the field
idWorkeris nullable – it will not see the rows where fieldidWorker IS NULL.The ultimate solution to this looks weird but it works:
Now it’ll see
NULLfields too.If it is bad idea to sth like that (I can see probable performance impact – 3 times a row it does the
IFNULLcondition) – please correct me.