tags:

views:

77

answers:

1

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.

A: 

All you doing is this single select? What's the point in wrapping it in stored procedure, then? Use prepared statement (do NOT build dynamic SQL) in your application and supply list of ids as parameter.

ChssPly76
I put it in a stored procedure because all the DB logic is in the db for this application, I want to keep things consistent, even still, who cares if a stored procedure only does a select, that doesnt make it any less of a stored procedure?
ForYourOwnGood
No, it doesn't. But it does create more work for you with no gain to show for it (perceived benefits like overall consistency notwithstanding). Stored procedures make sense when they perform complex tasks (requiring multiple independent selects, for example) in which case you may get better performance compared to doing the same in your application OR when they encompass certain logic (e.g. ensure data integrity beyond rules expressible via constraints). Either way, my answer stands - I would run this query from within the application without enclosing it in stored procedure.
ChssPly76