@HistoryMSBType => This is a variable which can contain any varchar.
Dep开发者_开发知识库ending upon it's value, i need to have different type of where clause.
How can I achieve this ?
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
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)
)
You need the ISNULL to make it match the CASE-ELSE exactly, but it won't matter if it can never be null.
精彩评论