使用IIF将Access查询转换为SQL Server

时间:2016-08-24 16:38:08

标签: sql sql-server ms-access

我正在寻找一些帮助将访问查询转换为sql server。

大多数情况下,我都很好,但我正在努力与IIF部分

SELECT * 
FROM   (([if_submission] 
     LEFT JOIN [payrollruns] 
            ON [if_submission].[periodnumber] = [payrollruns].[runid]) 
    LEFT JOIN [if_rti_finalfps_q] 
           ON [payrollruns].[runid] = [if_rti_finalfps_q].[runid]) 
   LEFT JOIN [if_rti_eps] 
          ON [if_submission].[submissionid] = [if_rti_eps].[submissionid] 
WHERE  [if_submission].[taxyear] = 2016 
   AND ( **IIF([if_submission].[submissiontypeid] = 8, 
         [payrollruns].[payemonth] = 5, 
               [if_submission].[periodnumber] = 5)** ) 
   AND ( [if_submission].[submissiontypeid] = 8 
          OR [if_submission].[submissiontypeid] = 10 ) 
   AND [if_submission].[testinlive] = 0 
ORDER  BY submissiontypeid 

这是我需要帮助的一点

IIF([if_submission].[submissiontypeid] = 8, 
         [payrollruns].[payemonth] = 5, 
               [if_submission].[periodnumber] = 5)

有人有什么想法吗?

由于

3 个答案:

答案 0 :(得分:0)

试试这个

AND 5 = CASE WHEN [if_submission].[submissiontypeid] = 8 
          THEN [payrollruns].[payemonth]
          ELSE [if_submission].[periodnumber]
        END

答案 1 :(得分:0)

您可以使用CASE expression

  

简单CASE表达式:

CASE input_expression   
    WHEN when_expression THEN result_expression [ ...n ]   
    [ ELSE else_result_expression ]   
END
     

搜索CASE表达式:

CASE  
    WHEN Boolean_expression THEN result_expression [ ...n ]   
    [ ELSE else_result_expression ]   
END  

答案 2 :(得分:0)

如果您使用SQL SERVER 2012+,则支持IIF,但语法必须略有不同。

IIF([if_submission].[submissiontypeid] = 8,[payrollruns].[payemonth],
                                           [if_submission].[periodnumber] ) = 5

否则我将使用AND/OR逻辑

执行此操作
......
(
(
[if_submission].[submissiontypeid] = 8 and [payrollruns].[payemonth] = 5
) 
or 
(
[if_submission].[periodnumber] = 5 and [if_submission].[submissiontypeid] <> 8
)
)
.....