尋找一個優雅的方式來解決這個...SQL和ALL操作
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!'
的問題是,如果(選擇ZIP從不同人的PERSONTYPE = 1)返回任何記錄,那麼上面的if計算爲真。我正在尋找一種方法,當ALL的子查詢沒有返回任何記錄時,將其評估爲false。
我目前的解決方案:
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服務器(T-SQL標準) – MooseCoder 2010-08-10 21:47:54