是否可以使用IF-ELSE或类似的东西做这样的事情:
SELECT
MemberID,
ProfileTypeID
PrCountryID, -- 3
PrStateID, -- 4
PrStateInt
FROM Member
WHERE PrCity IS NOT NULL
IF @ShowUnclaimed = 'N'
AND Claimed = 'Y'
AND SBIcon = 'N'
END
AND Viewable = 'Y'
AND SystemID = 2
非常感谢任何信息。
neojakey
答案 0 :(得分:5)
无需为此使用Case
SELECT
MemberID,
ProfileTypeID
PrCountryID, -- 3
PrStateID, -- 4
PrStateInt
FROM Member
WHERE PrCity IS NOT NULL
AND Viewable = 'Y'
AND SystemID = 2
AND ( @ShowUnclaimed != 'N'
OR ( Clamed = 'Y' AND SBIcon = 'N' )
)
答案 1 :(得分:1)
您正在寻找case
表达式(MSDN)
SELECT MemberID,
ProfileTypeID
PrCountryID, -- 3
PrStateID, -- 4
PrStateInt
FROM Member
WHERE PrCity IS NOT NULL
AND CASE
WHEN @ShowUnclaimed = 'N' AND Claimed = 'Y' AND SBIcon = 'N' THEN 1
END = 1
AND Viewable = 'Y'
AND SystemID = 2
答案 2 :(得分:1)
如果我理解您的意图,则无需IF
或CASE
。
这样的事情似乎就是你想要做的......?
WHERE PrCity IS NOT NULL
AND (@ShowUnclaimed = 'Y'
OR (Claimed = 'Y'
AND SBIcon = 'N'))
AND Viewable = 'Y'
AND SystemID = 2
答案 3 :(得分:0)
尝试使用CASE
:
SELECT
MemberID,
ProfileTypeID
PrCountryID, -- 3
PrStateID, -- 4
PrStateInt
FROM Member
WHERE PrCity IS NOT NULL
AND Viewable = 'Y'
AND SystemID = 2
AND Claimed = CASE WHEN @ShowUnclaimed = 'N' THEN 'Y' ELSE Claimed END
AND SBIcon = CASE WHEN @ShowUnclaimed = 'N' THEN 'N' ELSE SBIcon END
这只是一种方法。祝你好运。