I have a stored procedure that takes a comma-delimited string of IDs. I split them and put them into a temporary table and pull out records from another table using where id IN [table]
Is it ok to use this same procedure when only one id is passed in for the param? I could write a second stored procedure that would do exactly the samething but instead do where id = @id.
I have MANY stored procedures where multiple IDs or just one could be passed in. Do I try to reuse the existing procedures or write new ones? Is the performance hit significant?
You might like to try a JOIN instead of WHERE id IN – although I think you will get the same query plan.
So I assume you are doing:
in which case the equivalent JOIN syntax would be :
and in the second case whether there is 1, or many rows, it will be very effective provided [id] is indexed (I am assuming it is the PK on your table, and using a Clustered Index).
(Beware that if you have DUP IDs in @MyTempTable you will wind up getting dupes from MyTable as well 🙁 )
For best performance it would be worth explicitly declaring [id] as the PK on your temporary table (but given it only holds a few rows it probably won’t make much odds)