SQL: Return “true” if list of records exists?

后端 未结 14 876
情深已故
情深已故 2020-12-28 12:49

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

14条回答
  •  一个人的身影
    2020-12-28 13:13

    If the IN clause is a parameter (either to SP or hot-built SQL), then this can always be done:

    SELECT (SELECT COUNT(1)
              FROM product_a
             WHERE product_id IN (1, 8, 100)
           ) = (number of commas in product_id as constant)
    

    If the IN clause is a table, then this can always be done:

    SELECT (SELECT COUNT(*)
              FROM product_a
             WHERE product_id IN (SELECT Products
                                    FROM #WorkTable)
           ) = (SELECT COUNT(*)
                  FROM #WorkTable)
    

    If the IN clause is complex then either spool it into a table or write it twice.

提交回复
热议问题