In my database I'm searching for a list of ID's like this:
SELECT * from testcase where id in ('658', '659');
But how can I make this into a stored procedure, which can take multiple id's?
In this example I have only '658', '659'
but this could be more/less.
I made a stored procedure, but either it failes because I have two id's or more, and if I make it into a single string like this, '658, 659'
, then it only find the results from 658
:
CREATE DEFINER=`edmetrics`@`%` PROCEDURE `GetCaseFromStrList`(CaseIDs varchar(300))
BEGIN
IF CaseIDs != 0 OR CaseIDs IS NOT NULL THEN
Select testcase.id, testcase.TestCaseName
From testcase
Where testcase.id in (CaseIDs);
END IF;
END
Is there a way to make a call like this, with multiple X amount of id's?
CALL database.GetCaseFromStrList(658 , 659);
EDIT - more info
Was told I cold look at this solution, but it's not 100% the thing im looking for: StackOverflow Link
But I will show an example with pictures
When I write this SQL: SELECT * from testcase where id in ('658', '659');
I get the following result
If I try to implement the linked solution with FIND_IN_SET()
in my stored procedure I have this:
CREATE DEFINER=`edmetrics`@`%` PROCEDURE `GetCaseFromStrList`(CaseIDs varchar(300))
BEGIN
SET @idCaseIDs = CaseIDs;
IF CaseIDs != 0 OR CaseIDs IS NOT NULL THEN
Select testcase.id, testcase.TestCaseName
From testcase
WHERE FIND_IN_SET(testcase.id, @idCaseIDs);
END IF;
END
But if I use a string with all the ID's in it, and make a call like this:
CALL testreporting.GetCaseFromStrList('658, 659');
I get the following result
Table result from stored procedure
And in that result I'm missing the ID 659
.
and remember this is with two ID's, but I can have X amount of ID's not only two.