SQL Server 2012在条件IFF案例..WHEN

时间:2016-10-10 02:09:39

标签: sql-server-2012 where case-when

我正在使用SQL Server 2012.我正在尝试使用特定条件设置WHERE子句。 PAE.CaseStatusId包含值1,2,4,8,16。想法是使用Bitwise&选择WHERE的条件。例如,如果@CaseStatusSum = 3,则选择CaseStatusId IN(1,2);如果@CaseStatusSum = 4,则选择CaseStatusId = 4等。我有问题用IFF或CASE设置条件。我想知道是否还有解决这个问题的方法。任何输入都表示赞赏。谢谢。

CREATE PROCEDURE [dbo].[getPagingPatientLiteByNameTest]
(
@CaseStatusSum INT,
@FilterCriteria NVARCHAR(50),
@PageNo INT = 1,
@PageSize INT = 10,
@SortColumn NVARCHAR(40) = 'OperationDate',
@SortOrder NVARCHAR(4) = 'DESC'
)
AS
BEGIN

DECLARE
@lFilter NVARCHAR(50),
@lSortCol NVARCHAR(40),
@lFirstRec INT,
@lLastRec INT,
@lTotalRows INT,


SET NOCOUNT OFF
/* Setting local variable */
SET @lFilter = LTRIM(RTRIM(@FilterCriteria))
SET @lSortCol = LTRIM(RTRIM(@SortColumn))
SET @lFirstRec = (@PageNo - 1 ) * @PageSize + 1
SET @lLastRec = @PageNo * @PageSize
SET @lTotalRows = @lLastRec  - @lFirstRec + 1


; WITH CTE_Results 
AS (
SELECT ROW_NUMBER() OVER (ORDER BY
  CASE WHEN (@SortColumn = 'OperationDate' AND @lSortCol = 'ASC')
          THEN OpDate
  END ASC,
  CASE WHEN (@SortColumn = 'OperationDate' AND @lSortCol = 'DESC')
          THEN OpDate
  END DESC,
  CASE WHEN (@SortColumn = 'HN' AND @lSortCol = 'ASC')
          THEN HN
  END ASC,
  CASE WHEN (@SortColumn = 'HN' AND @lSortCol = 'DESC')
          THEN HN
  END DESC,
  CASE WHEN (@SortColumn = '')
          THEN OpDate
  END DESC
  ) AS ROWNUM,
    Count(*) over () AS TotalCount,
    Id,
    RecOut,
    CaseStatusId,
    LastName,
  OpDate
    FROM PAE
    WHERE 
        ( CaseStatusId = (
            CASE @CaseStatusSum
                WHEN 1 THEN 1
                WHEN 2 THEN 2
                WHEN 4 THEN 4
                WHEN 8 THEN 8
                WHEN 16 THEN 16
            END )

      /*  MS SQL Server 2012
          In the following commented section, neither IIF nor IF..ELSE work. 
          My goal is IF @CaseStatusSum = 3 THEN select CaseStatusSum IN (1, 2) and
          IF @CaseStatusSum = 8 THEN select CaseStatusSum IN (8) / CaseStatusSum = 8

      -- here is the problem, I am unable to get 1, 2 out of IIF to the IN condition
            OR CaseStatusId IN (IIF(@CaseStatusSum = 3, (1 2), 0)) 

      -- here is the problem: IF is not allow in this structure
            OR IF (@CaseStatusSum = 3)
                    BEGIN
                         CaseStatusId IN (1, 2)
                    END
              ELSE
                    BEGIN
                         CaseStatusId = 4
                    END
              END IF  
      */

            OR CaseStatusId = (
            CASE @CaseStatusSum
                WHEN 1 THEN 4
                WHEN 2 THEN 2
                WHEN 4 THEN 4
                WHEN 8 THEN 8
                WHEN 16 THEN 16
            END )
        )

        AND RecOut > getDate()
        AND (@lFilter IS NULL OR (LastName LIKE @lFilter))
)
 SELECT
  TotalCount,
    Id,
    RecOut,
    CaseStatusId,
    LastName,
  OpDate
FROM CTE_Results
WHERE 
  ROWNUM BETWEEN @lFirstRec AND @lLastRec
ORDER BY ROWNUM ASC


END

1 个答案:

答案 0 :(得分:0)

由于在任何给定时间最多有3个CaseStatusId场景,我可以在WHERE子句中使用OR。 案例1,2,4,8或16 - 具有一个独特的组合 案例3,5,6和7 - 具有多个组合。 案例3:组合1,3 案例5:组合1,4 案例6:组合:2,4 案例7:组合:1,2,4

Declare
@lCaseStatusId1 INT,
@lCaseStatusId2 INT,
@lCaseStatusId3 INT
:
:
SET NOCOUNT OFF
/* Setting local variable */
IF @AssignedTo < 0
    SET @lAssignedTo = null
ELSE
    SET @lAssignedTo = @AssignedTo

IF @CaseStatusSum = 16
BEGIN
    SET @lCaseStatusId1 = 16
    SET @lCaseStatusId2 = null
    SET @lCaseStatusId3 = null
END
ELSE IF @CaseStatusSum = 8
BEGIN
    SET @lCaseStatusId1 = 8
    SET @lCaseStatusId2 = null
    SET @lCaseStatusId3 = null
END
ELSE IF @CaseStatusSum = 7
BEGIN
    SET @lCaseStatusId1 = 4
    SET @lCaseStatusId2 = 2
    SET @lCaseStatusId3 = 1
END
ELSE IF @CaseStatusSum = 6
BEGIN
    SET @lCaseStatusId1 = 4
    SET @lCaseStatusId2 = 2
    SET @lCaseStatusId3 = null
END
ELSE IF @CaseStatusSum = 5
BEGIN
    SET @lCaseStatusId1 = 4
    SET @lCaseStatusId2 = 1
    SET @lCaseStatusId3 = null
END
ELSE IF @CaseStatusSum = 4
BEGIN
    SET @lCaseStatusId1 = 4
    SET @lCaseStatusId2 = null
    SET @lCaseStatusId3 = null
END
ELSE IF @CaseStatusSum = 3
BEGIN
    SET @lCaseStatusId1 = 2
    SET @lCaseStatusId2 = 1
    SET @lCaseStatusId3 = null
END
ELSE IF @CaseStatusSum = 2
BEGIN
    SET @lCaseStatusId1 = 2
    SET @lCaseStatusId2 = null
    SET @lCaseStatusId3 = null
END
ELSE IF @CaseStatusSum = 1
BEGIN
    SET @lCaseStatusId1 = 1
    SET @lCaseStatusId2 = null
    SET @lCaseStatusId3 = null
END
:
:
    WHERE  
        (CaseStatusId = @lCaseStatusId1
        OR CaseStatusId = @lCaseStatusId2
        OR CaseStatusId = @lCaseStatusId3)
            AND RecOut > getDate()
:
: