TSQL选项使用CASE语句的位置

时间:2015-08-06 15:39:49

标签: sql-server sql-server-2008 tsql stored-procedures

我有一个基本查询,用于根据在UI上选择的过滤器选项获取搜索结果。

在我的表中,我有一个名为expirationDate的日期列,它是文档过期的日期(不做任何事情,仅供参考)。

嗯,在用户界面中,可以选择要查看的状态;已过期或已过期。

根据您选择的那个,我需要更改查询但不知道如何处理它,因为它与日期有关,而不仅仅是它必须检查的状态列。

在下面的查询中,如果status = Active,我需要添加到WHERE子句,以便expirationDate尚未通过今天的日期(意味着它尚未过期)。但是,如果我选择过期,我需要WHERE子句包括expirationDate尚未通过今天日期的任何内容(活动项目)。

IF(@employee = '') SET @employee = NULL;
IF(@supervisor = '') SET @supervisor = NULL;
IF(@manager = '') SET @manager = NULL;
IF(@location = '') SET @location = NULL;
IF(@skillset = '') SET @skillset = NULL;
IF(@category = '') SET @category = NULL;
IF(@type = '') SET @type = NULL;
IF(@status = '') SET @status = NULL;

    SELECT recordID,
           employeeQID,
           firstName,
           lastName,
           department,
           location,
           supervisorQID,
           supervisorFirstName,
           supervisorLastName,
           managerQID,
           managerFirstName,
           managerLastName,
           documentationType,
           documentationCategory,
           CONVERT(VARCHAR(10), startDate, 101) AS startDate,
           CONVERT(VARCHAR(10), expirationDate, 101) AS expirationDate,
           notes,
           skillset,
           timestamp,
           creator
    FROM   dbo.documentation_records
    WHERE  (@employee IS NULL OR (employeeQID = @employee))
    AND    (@supervisor IS NULL OR (supervisorQID = @supervisor))
    AND    (@manager IS NULL OR (managerQID = @manager))
    AND    (@location IS NULL OR (location = @location))
    AND    (@skillset IS NULL OR (skillset = @skillset))
    AND    (@category IS NULL OR (documentationCategory = @category))
    AND    (@type IS NULL OR (documentationType = @type))
    FOR    XML PATH ('results'), TYPE, ELEMENTS, ROOT ('root');

处理此问题的最佳方法是同时允许状态为NULL并忽略如果未在UI中选择作为过滤器选项,就像我使用其他vars一样。

1 个答案:

答案 0 :(得分:-1)

        ((@status = '' 
     OR  @status IS NOT NULL && @STATUS = 'ACTIVE' AND EXPIRATIONDATE > GETDATE() )
     OR (@status IS NOT NULL && @STATUS = 'expired' AND EXPIRATIONDATE < GETDATE()))

以上是你的where子句的附加代码。它做到了。

**编辑** 如果状态未传入,则第一个或子句将返回所有内容。

||是什么让你在那里。你想要的东西不是NULL,IS ACTIVE,ISNT EXPIRED OR一切都不是NULL,已经过期,而且已经过期了。

IF(@employee = '') SET @employee = NULL;
IF(@supervisor = '') SET @supervisor = NULL;
IF(@manager = '') SET @manager = NULL;
IF(@location = '') SET @location = NULL;
IF(@skillset = '') SET @skillset = NULL;
IF(@category = '') SET @category = NULL;
IF(@type = '') SET @type = NULL;
IF(@status = '') SET @status = NULL;

    SELECT recordID,
           employeeQID,
           firstName,
           lastName,
           department,
           location,
           supervisorQID,
           supervisorFirstName,
           supervisorLastName,
           managerQID,
           managerFirstName,
           managerLastName,
           documentationType,
           documentationCategory,
           CONVERT(VARCHAR(10), startDate, 101) AS startDate,
           CONVERT(VARCHAR(10), expirationDate, 101) AS expirationDate,
           notes,
           skillset,
           timestamp,
           creator
    FROM   dbo.documentation_records
    WHERE  (@employee IS NULL OR (employeeQID = @employee))
    AND    (@supervisor IS NULL OR (supervisorQID = @supervisor))
    AND    (@manager IS NULL OR (managerQID = @manager))
    AND    (@location IS NULL OR (location = @location))
    AND    (@skillset IS NULL OR (skillset = @skillset))
    AND    (@category IS NULL OR (documentationCategory = @category))
    AND    (@type IS NULL OR (documentationType = @type))
    AND    (
                    (@status = ''
 OR @status IS NOT NULL AND @STATUS = 'ACTIVE' AND EXPIRATIONDATE > GETDATE() )
                 OR (@status IS NOT NULL AND @STATUS = 'expired' AND EXPIRATIONDATE < GETDATE())
                )
        FOR    XML PATH ('results'), TYPE, ELEMENTS, ROOT ('root');