Looking for an elegant way around this ...
DECLARE @ZIP INT SET @ZIP = 55555 IF @ZIP = ALL(SELECT ZIP FROM PEOPLE WHERE PERSONTYPE = 1) PRINT 'All people of type 1 have the same zip!' ELSE PRINT 'Not All people of type 1 have the same zip!'
The problem is that if (SELECT ZIP FROM PEOPLE WHERE PERSONTYPE = 1) does not return any records, then the above IF evaluates to true. I am looking for a way to make this evaluate to false when there are no records returned by the ALL subquery.
My current solution:
DECLARE @ZIP INT SET @ZIP = 55555 DECLARE @ALLZIPS TABLE (INT ZIP) INSERT INTO @ALLZIPS SELECT ZIP FROM PEOPLE WHERE PERSONTYPE = 1 IF EXISTS(SELECT TOP 1 * FROM @ALLZIPS) AND (@ZIP = ALL (SELECT ZIP FROM @ALLZIPS)) PRINT 'All people of type 1 have the same zip!' ELSE PRINT 'Not All people of type 1 have the same zip!'
source share