0

I have created a function in MYSQL as follow

CREATE FUNCTION `getTaskIds` ( typeIds TEXT) RETURNS TEXT
BEGIN
DECLARE output TEXT;
SELECT GROUP_CONCAT(id) INTO output FROM task WHERE task_id IN (typeIds );
RETURN output;
END

when I execute the function like SELECT getTaskIds( '1,2,3' ), It gives me the result for only task_id = 1. It does not condider task_id 2 and 3.

What am I missing in the function? Can you guys help me out?

Thanx in advance.

3
  • You might look at alternatives to passing a list of keys as a parameter, e.g. here Commented Oct 21, 2014 at 13:13
  • Sorry mate, I did not get your suggestion. Commented Oct 21, 2014 at 13:14
  • At present, you will need to use dynamic sql (e.g. prepared statemt ) to 'append' the list of task_ids to be retrieved. Unfortunately, MySql doesn't support Table Valued parameters (SqlServer) or Arrays (Oracle) either, leaving a further alternative of using Temporary tables on the connection. More here Commented Oct 21, 2014 at 13:17

2 Answers 2

2

You can't use IN since you are passing in a string. Try this FIND_IN_SET():

 SELECT GROUP_CONCAT(id) INTO output FROM task WHERE FIND_IN_SET(task_id, typeIds) > 0;
Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass all parameters in the function. So if you want 3 parameters:

CREATE FUNCTION `getTaskIds` ( typeId1 TEXT, typeId2 TEXT, typeId3 TEXT) RETURNS TEXT
BEGIN
DECLARE output TEXT;
// do what you want to do
RETURN output;
END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.