SWITCH 参数量不正确 - 看不到哪里

SWITCH Incorrect Argument Amount - Can't See Where

我正在尝试使用 MS Access 中的 SWITCH 函数,但我一直被告知参数数量不正确。我是 switch 函数的新手,但我理解语法。

切换(表达式 1、值 1、表达式 2、值 2、... expression_n、value_n)

我是不是在开关中做了一些不能使用的事情,也许是子查询?我检查了我的逗号、括号,发现有一个表达式,然后是一个 return 值。当我从 T-SQL 转换为 jet 并替换一个工作得很好的 CASE 语句时,让我发疯。

Switch(
        (eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND eh.STARTDTE < sh.STARTDTE),
          "FAIL - Employment Start Date Before Service Start Date",
        (eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND eh.ENDDTE > sh.ENDDTE),
          "FAIL - Employment End Date After Service End Date",
        (eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND  (
                                                                                            SELECT
                                                                                                COUNT(*)
                                                                                            FROM
                                                                                                emphist AS eh2
                                                                                            WHERE
                                                                                                eh2.MEMBNO = sh.MEMBNO
                                                                                            AND eh2.EMPID = sh.EMPID
                                                                                            AND eh2.ENDDTE = DateAdd(dd,-1,eh.STARTDTE)
                                                                                          ) = 0),
          "FAIL - Previous Employment Period Corrupt or Missing",
        (eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND  (
                                                                                            SELECT
                                                                                                COUNT(*)
                                                                                            FROM
                                                                                                emphist AS eh2
                                                                                            WHERE
                                                                                                eh2.MEMBNO = sh.MEMBNo
                                                                                            AND eh2.EMPID = sh.EMPID
                                                                                            AND eh2.ENDDTE = DateAdd(dd,-1,eh.STARTDTE)
                                                                                          ) = 0),
          "FAIL - Previous Employment Period Corrupt or Missing",
        (eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND (
                                                                                            SELECT
                                                                                                COUNT(*)
                                                                                            FROM
                                                                                                emphist AS eh2
                                                                                            WHERE
                                                                                                eh2.MEMBNO = sh.MEMBNO
                                                                                            AND eh2.EMPID = sh.EMPID
                                                                                            AND eh2.STARTDTE = DateAdd(dd,1,eh.ENDDTE)
                                                                                          ) = 0),
          "FAIL - Next Employment Period Corrupt or Missing",
        (eh.STARTDTE = sh.STARTDTE AND eh.ENDDTE <> sh.ENDDTE AND (
                                                                    SELECT
                                                                        COUNT(*)
                                                                    FROM
                                                                        emphist AS eh2
                                                                    WHERE
                                                                        eh2.MEMBNO = sh.MEMBNO
                                                                    AND eh2.EMPID = sh.EMPID
                                                                    AND eh2.STARTDTE = DateAdd(dd,1,eh.ENDDTE)
                                                                  ) = 0),
          "FAIL - Next Employment Period Corrupt or Missing",
        (eh.STARTDTE <> sh.STARTDTE AND eh.ENDDTE = sh.ENDDTE AND (
                                                                    SELECT
                                                                        COUNT(*)
                                                                    FROM
                                                                        emphist AS eh2
                                                                    WHERE
                                                                        eh2.MEMBNO = sh.MEMBNO
                                                                    AND eh2.EMPID = sh.EMPID
                                                                    AND eh2.ENDDTE = DateAdd(dd,-1,eh.STARTDTE)
                                                                  ) = 0),
          "FAIL - Previous Employment Period Corrupt or Missing",
        (eh.STARTDTE = sh.STARTDTE AND eh.ENDDTE = sh.ENDDTE AND  (
                                                                    SELECT
                                                                        COUNT(*)
                                                                    FROM
                                                                        emphist AS eh2
                                                                    WHERE
                                                                        eh2.MEMBNO = sh.MEMBNO
                                                                    AND eh2.EMPID = sh.EMPID
                                                                  ) <> 1),
          "FAIL - Too Many or No Employment History Records"
  ) AS "Reason"

已更新答案

Switch(
        (eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND eh.STARTDTE < sh.STARTDTE),
          "FAIL - Employment Start Date Before Service Start Date",
        (eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND eh.ENDDTE > sh.ENDDTE),
          "FAIL - Employment End Date After Service End Date",
        (eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND  (
                                                                                            SELECT
                                                                                                COUNT(*)
                                                                                            FROM
                                                                                                emphist AS eh2
                                                                                            WHERE
                                                                                                eh2.MEMBNO = sh.MEMBNO
                                                                                            AND eh2.EMPID = sh.EMPID
                                                                                            AND eh2.ENDDTE = DateAdd("dd",-1,eh.STARTDTE)
                                                                                          ) = 0),
          "FAIL - Previous Employment Period Corrupt or Missing",
        (eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND  (
                                                                                            SELECT
                                                                                                COUNT(*)
                                                                                            FROM
                                                                                                emphist AS eh2
                                                                                            WHERE
                                                                                                eh2.MEMBNO = sh.MEMBNo
                                                                                            AND eh2.EMPID = sh.EMPID
                                                                                            AND eh2.ENDDTE = DateAdd("dd",-1,eh.STARTDTE)
                                                                                          ) = 0),
          "FAIL - Previous Employment Period Corrupt or Missing",
        (eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND (
                                                                                            SELECT
                                                                                                COUNT(*)
                                                                                            FROM
                                                                                                emphist AS eh2
                                                                                            WHERE
                                                                                                eh2.MEMBNO = sh.MEMBNO
                                                                                            AND eh2.EMPID = sh.EMPID
                                                                                            AND eh2.STARTDTE = DateAdd("dd",1,eh.ENDDTE)
                                                                                          ) = 0),
          "FAIL - Next Employment Period Corrupt or Missing",
        (eh.STARTDTE = sh.STARTDTE AND eh.ENDDTE <> sh.ENDDTE AND (
                                                                    SELECT
                                                                        COUNT(*)
                                                                    FROM
                                                                        emphist AS eh2
                                                                    WHERE
                                                                        eh2.MEMBNO = sh.MEMBNO
                                                                    AND eh2.EMPID = sh.EMPID
                                                                    AND eh2.STARTDTE = DateAdd("dd",1,eh.ENDDTE)
                                                                  ) = 0),
          "FAIL - Next Employment Period Corrupt or Missing",
        (eh.STARTDTE <> sh.STARTDTE AND eh.ENDDTE = sh.ENDDTE AND (
                                                                    SELECT
                                                                        COUNT(*)
                                                                    FROM
                                                                        emphist AS eh2
                                                                    WHERE
                                                                        eh2.MEMBNO = sh.MEMBNO
                                                                    AND eh2.EMPID = sh.EMPID
                                                                    AND eh2.ENDDTE = DateAdd("dd",-1,eh.STARTDTE)
                                                                  ) = 0),
          "FAIL - Previous Employment Period Corrupt or Missing",
        (eh.STARTDTE = sh.STARTDTE AND eh.ENDDTE = sh.ENDDTE AND  (
                                                                    SELECT
                                                                        COUNT(*)
                                                                    FROM
                                                                        emphist AS eh2
                                                                    WHERE
                                                                        eh2.MEMBNO = sh.MEMBNO
                                                                    AND eh2.EMPID = sh.EMPID
                                                                  ) <> 1),
          "FAIL - Too Many or No Employment History Records"
  ) AS "Reason"

引发错误的不是 Switch,而是 IsNull。所以替换所有这些错误的语句:

IsNull(eh.ENDDTE,"")

与:

Nz(eh.ENDDTE)

好的,有两个问题。

  1. IsNull 函数不是 Access 函数,因此我用 NZ 替换(相同的语法)。

  2. 对于 DateAdd 函数,我忽略了用语音标记将 datepart 参数括起来。

查询现在可以正常运行了。我猜是因为 IsNull 函数没有被识别,所以看起来好像缺少了我认为是的参数。吸取教训,参数数量不正确也可能表示函数无效。

注意。我会用修改后的 switch 语句更新我的 OP,供其他人比较。