Vous cherchez une façon élégante de contourner ce ...SQL et ALL opérateur
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!'
Le problème est que, si (SELECT postal DE PERSONNES OU PersonType = 1) ne renvoie aucun enregistrement, alors ce qui précède IF evalue à vrai. Je cherche un moyen de rendre cette évaluation à false quand il n'y a pas d'enregistrements retournés par la sous-requête ALL.
Ma solution actuelle:
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!'
MS SQL Server (norme T-SQL) – MooseCoder