ForYourOwnGood
ForYourOwnGood

Reputation: 40382

MySQL stored procedure parameters

I want to grab x amount of items out of the DB, I was wondering if there was a way of passing some list of numbers to a stored procedure to be used with WHERE IN?

SELECT item_id, item_description
FROM items
WHERE item_id IN ( NEED_LIST_OF_IDS_HERE );

Should I not use a stored procedure for this query and just build up the sql in the application?

Or should I make a seperate DB call for each single item_id?

NOTE: items is not the actual name of the table, so dont bash me for a poor name choice, Im just hiding implementation.

Upvotes: 1

Views: 619

Answers (1)

Alain Collins
Alain Collins

Reputation: 16362

Poor unanswered question from a year ago...

If you weren't using an IN clause, you could certainly generate/prepare the sql inside the stored procedure and execute it there. It's harder to parameterize the IN clause.

How about passing in the list of ids, make a temp table inside your stored procedure, and then join over to the items table?

Upvotes: 1

Related Questions