2017-09-16 25 views
0

阅读有关非常类似错误的其他文章,我相信我的情况有点不同。这里是代码:重复的SQL Server错误“在预期条件的上下文中指定的非布尔类型的表达式”。

DELETE FROM comp_system_location 
WHERE (system_id, location_id) IN 
    (
    SELECT 
     csl.system_id, 
     csl.location_id 
    FROM comp_system_location AS csl 
     INNER JOIN orgn_location AS ol ON csl.location_id = ol.location_id 
    WHERE 
     ol.building_id IN (1, 3, 4) 
     AND 
      (
      csl.system_id IN (29, 35) 
      AND csl.location_id NOT IN (40, 41, 43, 44, 46, 47) 
      ) 
     OR 
      (
      csl.system_id NOT IN (29, 35) 
      AND csl.location_id IN (40, 41, 43, 44, 46, 47) 
      ) 
    ); 

我觉得这有点不同的原因是我的比较WHERE子句是IN。错误是system_idWHERE (system_id,location_is) IN

从其他建议删除基于多列的行,这应该工作。但我以前错了...任何帮助,非常感谢!

回答

2

SQL Server不支持IN中的元组。您需要重写为EXISTSJOINEXISTS是最接近的,因为两者都代表半连接,所以如下所示。

WITH ToDelete 
    AS (SELECT csl.system_id, 
       csl.location_id 
     FROM comp_system_location AS csl 
       INNER JOIN orgn_location AS ol 
        ON csl.location_id = ol.location_id 
     WHERE ol.building_id IN (1, 3, 4) 
       AND (csl.system_id IN (29, 35) 
         AND csl.location_id NOT IN (40, 41, 43, 44, 
                46, 47)) 
       OR (csl.system_id NOT IN (29, 35) 
         AND csl.location_id IN (40, 41, 43, 44, 
               46, 47))) 
DELETE csl 
FROM comp_system_location csl 
WHERE EXISTS 
(
SELECT * 
FROM ToDelete td 
WHERE td.system_id = csl.system_id AND td.location_id = csl.location_id 
); 

这可能是不必要的CTE引用comp_system_location,但我不知道你的数据模型或所需的语义。

也许你只需要

DELETE csl 
FROM comp_system_location AS csl 
     INNER JOIN orgn_location AS ol 
     ON csl.location_id = ol.location_id 
WHERE ol.building_id IN (1, 3, 4) 
     AND (csl.system_id IN (29, 35) 
      AND csl.location_id NOT IN (40, 41, 43, 44, 
              46, 47)) 
     OR (csl.system_id NOT IN (29, 35) 
      AND csl.location_id IN (40, 41, 43, 44, 
             46, 47)); 

这不正是相同的语义您IN版本,但可能你正在尝试做什么?

+0

第二种选择是我最终选择的。谢谢你马丁!它工作完美。 –

相关问题