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

后端 未结 14 795
情深已故
情深已故 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:18

    Assuming you're using SQL Server, the boolean type doesn't exist, but the bit type does, which can hold only 0 or 1 where 0 represents False, and 1 represents True.

    I would go this way:

    select 1
        from Products
        where ProductId IN (1, 10, 100)
    

    Here, a null or no row will be returned (if no row exists).

    Or even:

    select case when EXISTS (
        select 1
            from Products
            where ProductId IN (1, 10, 100)
        ) then 1 else 0 end as [ProductExists]
    

    Here, either of the scalar values 1 or 0 will always be returned (if no row exists).

    0 讨论(0)
  • 2020-12-28 13:18

    If you have the IDs stored in a temp table (which can be done by some C# function or simple SQL) then the problem becomes easy and doable in SQL.

    select "all exist"
    where (select case  when count(distinct t.id) = (select count(distinct id) from #products) then "true" else "false" end
        from ProductTable t, #products p
        where t.id = p.id) = "true"
    

    This will return "all exists" when all the products in #products exist in the target table (ProductTable) and will not return a row if the above is not true.

    If you are not willing to write to a temp table, then you need to feed in some parameter for the number of products you are attempting to find, and replace the temp table with an 'in'; clause so the subquery looks like this:

    SELECT "All Exist"
    WHERE(
            SELECT case when count(distinct t.id) = @ProductCount then "true" else "false" 
            FROM ProductTable t 
            WHERE t.id in (1,100,10,20) -- example IDs
    ) = "true"
    
    0 讨论(0)
  • 2020-12-28 13:19

    Here's how I usually do it:

    Just replace your query with this statement SELECT * FROM table WHERE 1

       SELECT
          CASE WHEN EXISTS 
          (
                SELECT * FROM table WHERE 1
          )
          THEN 'TRUE'
          ELSE 'FALSE'
       END
    
    0 讨论(0)
  • 2020-12-28 13:28

    Your c# will have to do just a bit of work (counting the number of IDs passed in), but try this:

    select (select count(*) from players where productid in (1, 10, 100, 1000)) = 4
    

    Edit:

    4 can definitely be parameterized, as can the list of integers.

    If you're not generating the SQL from string input by the user, you don't need to worry about attacks. If you are, you just have to make sure you only get integers. For example, if you were taking in the string "1, 2, 3, 4", you'd do something like

    String.Join(",", input.Split(",").Select(s => Int32.Parse(s).ToString()))
    

    That will throw if you get the wrong thing. Then just set that as a parameter.

    Also, be sure be sure to special case if items.Count == 0, since your DB will choke if you send it where ParameterID in ().

    0 讨论(0)
  • 2020-12-28 13:31

    I know this is old but I think this will help anyone else who comes looking...

    SELECT CAST(COUNT(ProductID) AS bit) AS [EXISTS] FROM Products WHERE(ProductID = @ProductID)
    

    This will ALWAYS return TRUE if exists and FALSE if it doesn't (as opposed to no row).

    0 讨论(0)
  • 2020-12-28 13:33

    This may be too simple, but I always use:

    SELECT COUNT(*)>0 FROM `table` WHERE condition;
    
    0 讨论(0)
提交回复
热议问题