@HistoryMSBType =>这是一个可以包含任何varchar的变量。
根据它的价值,我需要有不同类型的where子句。
我怎样才能做到这一点?
SELECT * FROM stageTable map
WHERE id = 1
CASE @HistoryMSBType
WHEN 'Utilization Other' THEN
AND map.ColumnID = 4
WHEN 'Cost Other' THEN
AND map.ColumnID = 6
ELSE
AND map.ColumnName = @HistoryMSBType
END
答案 0 :(得分:3)
SELECT *
FROM stageTable map
WHERE id = 1
AND (
(@HistoryMSBType = 'Utilization Other' AND map.ColumnID = 4)
OR
(@HistoryMSBType = 'Cost Other' AND map.ColumnID = 6)
OR
(isnull(@HistoryMSBType,'') NOT IN ('Utilization Other','Cost Other')
AND map.ColumnName = @HistoryMSBType)
)
你需要 ISNULL 才能使它与CASE-ELSE完全匹配,但如果它永远不会为空则无关紧要。