MSSQL:“!”附近的语法不正确

MSSQL : Incorrect syntax near '!'

SELECT COUNT(*) AS numrows
FROM "tbltasks"
WHERE "status" = 1
  AND CASE WHEN rel_type='project'
        AND rel_id IN (SELECT project_id
                      FROM tblproject_settings
                      WHERE project_id = rel_id
                       AND "name" = 'hide_tasks_on_main_tasks_table'
                       AND value=1)
       THEN rel_type != 'project'
       ELSE 1 = 1 END

我正在处理现有项目,我正在将查询从 MySQL 转换为 MsSQL,当我执行此查询时,它给出:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '!'.

卡在这个查询里整整一周了,找不到问题出在哪里,真不知道该怎么办。

首先,修复可能不需要的引号。其次,问题可能是CASE/THEN中的布尔表达式。您可以改用:

SELECT COUNT(*) AS numrows
FROM tbltasks
WHERE status = 1 AND
      (CASE WHEN rel_type = 'project' AND
                 rel_id IN (SELECT project_id
                            FROM tblproject_settings
                            WHERE project_id = rel_id AND
                                  name = 'hide_tasks_on_main_tasks_table' AND
                                  value = 1
                           )
            THEN 0 ELSE 1
       END) = 1

我不喜欢在 WHERE 子句中使用 CASE 表达式,因此我建议重写该代码。但是,为了两个数据库中的查询之间的一致性,我会等到所有代码都能正常工作后再做这样的改进。