开发者

how can I have different where condition using case statement?

开发者 https://www.devze.com 2023-02-18 02:11 出处:网络
@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.

@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.

0

精彩评论

暂无评论...
验证码 换一张
取 消