2017-09-16 51 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

第二種選擇是我最終選擇的。謝謝你馬丁!它工作完美。 –

相關問題