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

后端 未结 14 796
情深已故
情深已故 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:34
    // not familiar with C#, but C#'s equivalent of PHP's:
    $count = count($productIds); // where $productIds is the array you also use in IN (...)
    
    SELECT IF ((SELECT COUNT(*) FROM Products WHERE ProductID IN (1, 10, 100)) = $count, 1, 0)
    0 讨论(0)
  • 2020-12-28 13:39
    DECLARE @values TABLE (ProductId int)
    INSERT @values (1)
    INSERT @values (10)
    INSERT @values (100)
    
    SELECT CASE WHEN (SELECT COUNT(*) FROM @values v) = 
                     (SELECT COUNT(*) FROM Products p WHERE p.ProductId IN
                           (SELECT v.ProductId FROM @values v))
                THEN CAST(1 AS bit)
                ELSE CAST(0 AS bit)
           END [AreAllFound]
    
    0 讨论(0)
提交回复
热议问题