2016-10-10 70 views
0

我正在使用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。我想知道是否有解決這個問題的方法。任何輸入讚賞。謝謝。SQL Server 2012在哪裏條件IFF Case..WHEN

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 

回答

0

由於任何給定時間最多有3個CaseStatusId場景,所以我可以在那裏使用OR或WHERE子句。 案例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() 
: 
: