在期望条件的上下文中指定的非布尔类型的表达式

时间:2016-08-05 11:07:26

标签: sql-server tsql

SELECT HD.Login_ID
    ,HD.Site_Group
    ,HD.Incident_Number
    ,HD.Internet_E_mail
    ,HD.Assigned_Group
    ,HD.Assignee
    ,HD.Status_Reason
    ,HD.Description
    ,HD.Detailed_Decription
    ,HD.Priority
    ,HD.Submit_Date
    ,HD.STATUS
    ,HD.Entry_ID
    ,HD.Service_Type
    ,(SM.SVTDueDate - DATEDIFF(second, '1970-01-01 00:00:00', GETUTCDATE())) / 60 / 60 AS SLM_HOURS
    ,(SM.SVTDueDate - DATEDIFF(second, '1970-01-01 00:00:00', GETUTCDATE())) / 60 AS SLM_MINUTES
    ,SM.SVTDueDate
    ,WL.Detailed_Description AS Work_Log_Description
    ,WL.Work_Log_Submitter
    ,DATEADD(hour, 14, DATEADD(day, 25567, WL.Work_Log_Submit_Date / (60.0 * 60.0 * 24.0) - 0.25)) AS WORKLOGDATE
    ,(DATEDIFF(second, '1970-01-01 00:00:00', GETUTCDATE()) - WL.Work_Log_Submit_Date) / 60 / 60 AS WORK_AGE_HOURS
    ,(DATEDIFF(second, '1970-01-01 00:00:00', GETUTCDATE()) - WL.Work_Log_Submit_Date) / 60 AS WORK_AGE_MINUTES
    ,CASE HD.SLM_STATUS
        WHEN 2
            THEN 'Warning'
        WHEN 3
            THEN 'Breached'
        ELSE 'Within'
        END AS SLM_STATUS
    ,rsw.work_shift_code
    ,HD.InstanceId
    ,HD.Assignee_Login_ID
    ,(DATEDIFF(second, '1970-01-01 00:00:00', GETUTCDATE()) - AL.Audit_Date) / 60 AS ASSIGNED_AGE_MINUTES
FROM HPD_Help_Desk AS HD
LEFT OUTER JOIN SLM_Measurement AS SM ON HD.Incident_Number = SM.ApplicationUserFriendlyID
LEFT OUTER JOIN HPD_WorkLog AS WL ON HD.Incident_Number = WL.Incident_Number
LEFT OUTER JOIN reference.dbo.SAP_worker AS rsw ON HD.Internet_E_mail = LOWER(rsw.micron_username) + '@micron.com'
LEFT OUTER JOIN HPD_HelpDesk_AuditLogSystem AS AL ON HD.Entry_ID = AL.Original_Request_ID
WHERE (HD.Assigned_Group LIKE '%MSC%')
    AND (
        HD.STATUS IN (
            '1'
            ,'2'
            ,'3'
            )
        )
    AND HD.Assignee IS NOT NULL
    AND (
        WL.Work_Log_Submit_Date IS NULL
        OR WL.Work_Log_ID = (
            SELECT TOP 1 (Work_Log_ID) AS Expr1
            FROM HPD_WorkLog AS ML
            WHERE (Incident_Number = HD.Incident_Number)
            ORDER BY Submit_Date DESC
            )
        )
    AND (
        HD.Assigned_Group NOT IN (
            'MIL_MSC'
            ,'TJP_MSC'
            ,'MIT_MSC'
            ,'MSL_MSC'
            )
        )
    AND (HD.Assigned_Group IN (@Site_Name))
    AND (
        HD.Status_Reason <> 12000
        OR HD.Status_Reason IS NULL
        )
    AND (HD.Assignee_Login_ID IN @Assignee_List)
    AND (
        AL.Audit_date IS NULL
        OR AL.Audit_date = (
            SELECT MAX(Audit_date) AS Expr1
            FROM HPD_HelpDesk_AuditLogSystem AS ALS
            WHERE Fields_Changed LIKE ('%Assignee%')
                AND (HD.Entry_ID = Original_Request_ID)
            )
        AND (
            SM.Create_Date IS NULL
            OR SM.Create_Date = (
                SELECT TOP 1 Create_Date
                FROM SLM_Measurement
                WHERE (ApplicationUserFriendlyID = HD.Incident_Number)
                ORDER BY Create_Date DESC
                )
            )
        )

1 个答案:

答案 0 :(得分:2)

我在这段代码中看到的唯一明显的错误是这一行:

HD.Assignee_Login_ID IN @Assignee_List

我不确定它是否会产生确切的错误,但它可能会。

首先,IN的语法需要括号:

HD.Assignee_Login_ID IN (@Assignee_List)

但是,这不是你想要的。不幸的是,您无法将列表作为参数插入。

你基本上有三个选择:

  • 使用动态SQL。
  • 使用多重比较(HD.Assignee_Login_ID IN (@Assignee_List1, @Assignee_List2, @Assignee_List3)
  • 使用LIKE

后者看起来像:

',' + @Assignee_List + ',' LIKE '%,' + CAST(@Assignee_List as VARCHAR(255)) + ',%'