user3114347 user3114347 - 4 months ago 8
SQL Question

SQL query if more than 1 row exsists return 0

Maybe someone can help me with the following problem. I have a query which returns records.

But when it has 1 or more rows, I want to receive 0 rows. I think the query should look like:

IF EXISTS(SELECT * FROM Names nm WHERE nm.Username = '%a%')
BEGIN
-- I should return 0 rows if the query above has rows
END

Answer

Below code might help:

IF EXISTS(SELECT * FROM Names nm WHERE nm.Username = '%a%')
BEGIN
        -- It should return 0 rows if the query above has rows
        SELECT * FROM Names nm WHERE 1=2;

END
Comments