Question : SQL IN subquery vs another method, chained where

Hi,

SQL SERVER 2008

I am wondering which way to go. I need to filter on a given set of Ids. (via .net code).

SELECT * FROM TABLE WHERE ParentID IN (1, 2, 3, 4, 5, 6, 7, 8, 9 10)

or, is it better to write like this

SELECT FROM TABLE WHERE ParentID = 1 OR ParentID = 2 OR ParentID = 3 OR ParentID = 4 OR ParentID = 5 OR ParentID = 6 OR ParentID = 71 OR ParentID = 8 OR ParentID = 9 OR ParentID = 10

or is there a better join?

The sql will use parameters.

Given that the IDs provided are dynamic and can range from 1 - n and will average 4.

Also, that ParentID is a foreign key, there could be as many matching any of the numbers given. E.g., given 1, there could be 10 records returned. But again, it is unknown how many records there will be. I will placeindexes as required.

thanks

Answer : SQL IN subquery vs another method, chained where

SQL Server will expand an IN clause into OR components (functionally equivalent).
Typing it out manually will simply be insane for say 25 items...
Using ORs is also susceptible to errors when you start mixing it with outer condition like AND if you don't bracket properly.
Random Solutions  
 
programming4us programming4us