MySQL stored procedure with variable argument list
mysql
Solution
AFAIK, you can't have a variable argument list like that. You can do one of a couple of things:
Take a fixed maximum number of parameters, and check them for null-ness before concatenating:
CREATE PROCEDURE SP_rptProvince2(a1 VARCHAR(45), a2 VARCHAR(45), ...)
...
IF a1 IS NOT NULL THEN
SET @strQuery = CONCAT(@strQuery, ' AND ', a2);
END IF;
If you need predetermined fields to which the criteria in the argument apply (like the `e_Region` parameter in your existing code), then you modify the CONCAT operation appropriately.
Possible invocation:
CALL SP_rptProvince2('''North''', 'column3 = ''South''')
Take a single parameter that is much bigger than just 45 characters, and simply append it to the query (assuming it is not null).
Clearly, this places the onus on the user to provide the correct SQL code.
Possible invocation:
CALL SP_rptProvince2('RegionName = ''North'' AND column3 = ''South''')
There's not a lot to choose between the two. Either can be made to work; neither is entirely satisfactory.
You might note that there was a need to protect the strings in the arguments with extra quotes; that is the sort of thing that makes this problematic.
Problem
I have made a stored procedure. I want it to filter the data by different parameters. If I pass one parameter, it should be filtered by one; if I pass two, it should be filtered by two, and so on, but it is not working. Can anyone help me please? ``` DROP PROCEDURE IF EXISTS medatabase.SP_rptProvince2; CREATE PROCEDURE medatabase.`SP_rptProvince2`( IN e_Region VARCHAR(45) ) BEGIN DECLARE strQuery VARCHAR(1024); DECLARE stmtp VARCHAR(1024); SET @strQuery = CONCAT('SELECT * FROM alldata where 1=1'); IF e_region IS NOT NULL THEN SET @strQuery = CONCAT(@strQuery, ' AND (regionName)'=e_Region); END IF; PREPARE stmtp FROM @strQuery; EXECUTE stmtp; END; ```