Is a multi-valued stored procedure parameter just bad practice?
I have the strange aversion to passing in multiple ID par开发者_StackOverflow社区ameters to a single stored procedure. For example, this feels just wrong:
GetMyObject(ListofIDs, OtherParam1, OtherParam2, ...)
I understand HOW to do it (correctly if I must).. but I don't feel like I should do it. I feel like it defeats the purpose of a "get item" stored procedure/sub routine. I feel like I should build my SPs to support appropriate filter parameters. If my caller has a list of IDs, shouldn't they call the sp that many times?
Help?
A "get item by ID" routine should never return more than one object, because that makes absolutely no linguistic sense.
A "get items by IDs" routine? Sure, if you have a decent use-case for it and it'll be used often enough.
But most of the time, yes, instead of a routine returning multiple items by ID, you want a routine that returns items based on application-appropriate filtering parameters (e.g. "give me all transactions from January 8th for more than $10").
By the way, sometimes a range of IDs (e.g. everything between 5 and 10) is a perfectly valid set of filters!
Incidentally, this isn't necessarily just a MySQL or SQL-in-general issue. Almost any sort of dataset querying API in any language will present these same design questions, and their answers will usually be highly similar.
精彩评论