2

我的存储过程接受参数,其中一个@SearchBy可以包含几个不同的值,如IISAppPool, ScheduledTask, WindowsService, COM+,ServiceAccount等,如下CASE statement所示。

根据值是什么,它将在值的相应列中查找该值。

sComponentType将始终包含IIS_APP_POOL、或SCHED_TASK,无论是什么。WIN_SERVICECOM_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

我希望我说得通。

4

2 回答 2

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 不为空

于 2013-09-04T09:15:50.107 回答
0

你能不使用搜索的case语句而不是简单的case语句吗?例如

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 + '%')
于 2013-09-04T09:17:24.767 回答