切换不正确的参数金额 - 无法查看位置

时间:2017-10-31 16:08:56

标签: ms-access ms-access-2007 jet-sql

我正在尝试在MS Access中使用SWITCH功能,我不断被告知没有正确数量的参数。我是切换功能的新手,但我理解语法。

Switch(expression1,value1,expression2,value2,... expression_n,value_n)

我是否在交换机中做了一些无法使用的内容,可能是子查询?我检查了我的逗号,括号,并且有一个表达式,然后是一个返回值。当我从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"

2 个答案:

答案 0 :(得分:0)

不是 Switch ,而是 IsNull 会引发错误。所以要替换所有这些错误的陈述:

IsNull(eh.ENDDTE,"")

使用:

Nz(eh.ENDDTE)

答案 1 :(得分:0)

好的,有两个问题。

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

  2. 对于DateAdd函数,我忽略了用语音标记包围datepart参数。

  3. 现在,查询很愉快。我猜测因为IsNull函数没有被识别,看起来似乎缺少了我认为的那个参数。获得的经验教训,不正确的参数数量也可能表示无效的功能。

    NB。我将使用修改后的switch语句更新我的OP,供其他人进行比较。