SQL: Return "true" if list of records exists?

Posted by User on Stack Overflow See other posts from Stack Overflow or by User
Published on 2010-05-19T00:23:07Z Indexed on 2010/05/19 0:30 UTC
Read the original article Hit count: 572

Filed under:
|
|
|
|

An alternative title might be: Check for existence of multiple rows?

Using a combination of SQL and C# I want a method to return true if all products in a list exist in a table. If it can be done in all in SQL that would be preferable. I have written a method that returns whether a single productID exists using the following SQL:

SELECT productID FROM Products WHERE ProductID = @productID

If this returns a row, then the c# method returns true, false otherwise.

Now I'm wondering if I have a list of product IDs (not a huge list mind you, normally under 20). How can I write a query that will return a row if all the product id's exist and no row if one or more product id's does not exist?

(Maybe something involving "IN" like:
SELECT * FROM Products WHERE ProductID IN (1, 10, 100))

© Stack Overflow or respective owner

Related posts about sql

Related posts about c#