我的存储过程接受参数,其中一个是@SearchBy
,其中可以包含几个不同的值,例如IISAppPool
,ScheduledTask
,WindowsService
,COM+
,ServiceAccount
等,如下面的CASE statement
所示。
根据值的不同,它将在值的相应列中查找该值。
无论sComponentType
是什么, IIS_APP_POOL
将始终包含SCHED_TASK
,WIN_SERVICE
,COM_PLUS
或@SearchBy
。
我正在努力使用where子句,它需要一个CASE
语句。
以下工作正常。
WHERE
sDate BETWEEN @FromDate AND @ToDate
AND
CASE @SearchBy
WHEN 'IISAppPool' THEN sComponentName
WHEN 'ScheduledTask' THEN sComponentName
WHEN 'WindowsService' THEN sComponentName
WHEN 'COM+' THEN sComponentName
WHEN 'ServiceAccount' THEN sServiceAccount
WHEN 'Server' THEN sServerName
WHEN 'IMCCReference' THEN sIMCCReference
WHEN 'User' THEN sManager
END LIKE('%' + @SearchCriteria + '%')
我正在寻找的是:
WHERE
sDate BETWEEN @FromDate AND @ToDate
AND
CASE @SearchBy
WHEN 'IISAppPool' THEN sComponentName AND sComponentType = 'IIS_APP_POOL'
WHEN 'ScheduledTask' THEN sComponentName AND sComponentType = 'SCHED_TASK'
WHEN 'WindowsService' THEN sComponentName AND sComponentType = 'WIN_SERVICE'
WHEN 'COM+' THEN sComponentName AND sComponentType = 'COM_PLUS'
WHEN 'ServiceAccount' THEN sServiceAccount
WHEN 'Server' THEN sServerName
WHEN 'IMCCReference' THEN sIMCCReference
WHEN 'User' THEN sManager
END LIKE('%' + @SearchCriteria + '%')
我知道这是不可能的,所以除了上面的内容之外,我还尝试了以下内容,但如果@SearchBy
不是应用程序池,计划任务,Windows服务,我只是不知道如何编写代码或COM +组件:
AND
sComponentType =
CASE @SearchBy
WHEN 'IISAppPool' THEN 'IIS_APP_POOL'
WHEN 'ScheduledTask' THEN 'SCHED_TASK'
WHEN 'WindowsService' THEN 'WIN_SERVICE'
WHEN 'COM+' THEN 'COM_PLUS'
END
我希望我有意义。
答案 0 :(得分:1)
将其更改为
sComponentType =
CASE @SearchBy
WHEN 'IISAppPool' THEN 'IIS_APP_POOL'
WHEN 'ScheduledTask' THEN 'SCHED_TASK'
WHEN 'WindowsService' THEN 'WIN_SERVICE'
WHEN 'COM+' THEN 'COM_PLUS'
ELSE sComponentType
END
假设sComponentType不为空
答案 1 :(得分:0)
您是否可以使用搜索的案例陈述而不是简单的案例陈述? e.g。
AND
CASE
WHEN @SearchBy = 'IISAppPool' AND sComponentType = 'IIS_APP_POOL' THEN sComponentName
WHEN @SearchBy = 'ScheduledTask' AND sComponentType = 'SCHED_TASK' THEN sComponentName
WHEN @SearchBy = 'WindowsService' AND sComponentType = 'WIN_SERVICE' THEN sComponentName
WHEN @SearchBy = 'COM+' AND sComponentType = 'COM_PLUS' THEN sComponentName
WHEN @SearchBy = 'ServiceAccount' THEN sServiceAccount
WHEN @SearchBy = 'Server' THEN sServerName
WHEN @SearchBy = 'IMCCReference' THEN sIMCCReference
WHEN @SearchBy = 'User' THEN sManager
END LIKE('%' + @SearchCriteria + '%')