使用SQL

时间:2015-07-30 08:21:35

标签: mysql sql sql-server sql-server-2008 tsql

尝试将以下查询转换为SQL,查询在MySQL上正常工作。问题似乎是CASE WHEN区域字段我得到相同的错误。

  

显示消息102,级别15,状态1,行44' ='附近的语法不正确。   消息156,级别15,状态1,行47关键字附近的语法不正确   '与&#39 ;.消息156,级别15,状态1,行49语句附近的语法不正确   关键字' ELSE'。

WHEN T.[StatusID] = 3
            THEN  
                CASE WHEN (((SELECT COUNT(TA1.[Approver_ID]) FROM [QESTORM].[dbo].[CR_TicketApproval] TA1
                    INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_SubRoute] CFSR1 ON TA1.[SubRoute_ID] = CFSR1.[ID]
                    WHERE TA1.[Ticket_ID]= @iTkID AND TA1.Active=1  AND CFSR1.Active=1  AND CFSR1.[Sequence] =(SELECT CFSR2.[Sequence] FROM [QESTORM].[dbo].[CR_Ticket] T2  INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_SubRoute] CFSR2 ON T2.[SubRouteID] = CFSR2.[ID]
                    WHERE T2.[ID] = @iTkID))<(SELECT COUNT(DISTINCT CFSR1.[ID])FROM [QESTORM].[dbo].[CR_Ticket]  AS T1     INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_Route]  AS CFR1  ON T1.[FormID] = CFR1.[FormID] INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_SubRoute]  AS CFSR1  ON CFR1.[ID] = CFSR1.[RouteID]
                    WHERE CFR1.[Active] = 1 AND CFSR1.[Active] = 1 AND T1.[ID] = @iTkID AND CFSR1.[Category] = 1 AND  CFSR1.[Sequence] = ( SELECT CFSR2.[Sequence] FROM [QESTORM].[dbo].[CR_Ticket]  AS T2 INNER JOIN [QESTORM].[dbo].[CR_ControlFlow_SubRoute]  AS CFSR2 ON T2.[SubRouteID] = CFSR2.[ID]
                    WHERE T2.[ID] = @iTkID))))

                THEN
                    CASE WHEN ((SELECT COUNT(1) FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID]=@iTkID And [Active]=1) = 0) 
                        THEN 
            --ERROR SHOW HERE =>    ((T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin)
                            AND nx.actor=3 
                            AND srSameSeq.subRouteID NOT IN (SELECT subRouteID FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID]=@iTkID AND Active=1 )
                            AND appSameSeq.NTLogin=in_NTLogin 
                            AND nx.actor=3 AND srSameSeq.subRouteID NOT IN (SELECT subRouteID FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID] = @iTkID AND Active = 1)
                        ELSE 0
                    END

3 个答案:

答案 0 :(得分:1)

我会把帽子扔进戒指。

该SQL语句可能有多个错误。我要指出的是:

 CASE WHEN ((SELECT COUNT(1) FROM [QESTORM].[dbo].  [CR_TicketApproval] WHERE [Ticket_ID]=@iTkID And [Active]=1) = 0) 
     THEN 
        --ERROR SHOW HERE =>    ((T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin)
                        AND nx.actor=3 
                        AND srSameSeq.subRouteID NOT IN (SELECT subRouteID FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID]=@iTkID AND Active=1 )
                        AND appSameSeq.NTLogin=in_NTLogin 
                        AND nx.actor=3 AND srSameSeq.subRouteID NOT IN (SELECT subRouteID FROM [QESTORM].[dbo].[CR_TicketApproval] WHERE [Ticket_ID] = @iTkID AND Active = 1)
                    ELSE 0
                END

您是否正在尝试评估条件表达式,并将结果返回为1或0,就好像它是编程语言中的布尔表达式一样?

这在TSQL中不起作用。这种表达评价:

SET  @value = (1 > 0)

...会产生错误。您无法评估条件表达式:您只能在测试中使用它,例如WHEREHAVINGWHEN子句。

所以,如果你正在做的事情,你可能会更好地将条件评估包装在另一个CASE语句中,如下所示:

THEN
    CASE WHEN  {complex conditional statement}
         THEN  1
         ELSE  0
    END
ELSE 
    0
END

另一件事:这是一个复杂的查询语句!我没有分析它是否可以简化,但我建议你这样做,着眼于使用Common Table Expressions代替一些子查询。这可以使查询更容易理解(和调试)。

答案 1 :(得分:0)

我们缺少完整的查询,但看起来你打开了太多的括号。

如果您查看显示错误的行:

((T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin)

您打开2个括号,但只关闭一个。

这就是为什么你会在else附近收到错误的原因,因为你需要关闭第二个括号才能获得else

所以你需要

((T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin))

(T.[AuditUser_ID] = '444' OR T.[AuditUser_ID] IS NULL) AND (nx.actor = 2 OR appSameSeq.NTLogin=in_NTLogin)

在那一行

答案 2 :(得分:0)

看起来您的特定行上的case语句没有“返回值”。

当您展开CASE WHEN声明时,您会遇到以下情况:

CASE WHEN <something> THEN
  CASE WHEN <something else> THEN
    CASE WHEN <something else again> THEN
      -- THEN WHAT ?
    ELSE 
      0
    END
  END
END

不是在-THEN WHAT点上放置一个值,而是放入另一个条件语句。你必须在这里选择一个值。