Oracle 与 MS SQL UPDATE 使用 IN SELECT

Oracle vs MS SQL UPDATE using IN SELECT

此 SQL 在 MS SQL 服务器上工作正常,但会产生错误 "ORA-00907: missing right parenthesis"。

SQL:

UPDATE DELIVERY
SET VISIBLE = 0
WHERE DELIVERY.ID
IN 
(
    SELECT DELIVERY.ID FROM delivery WHERE DELIVERY.VISIBLE = 1
    EXCEPT
        SELECT DELIVERY.ID FROM delivery LEFT JOIN inventory ON INVENTORY.DELIVERYID = DELIVERY.ID
            WHERE ((DELIVERY.VISIBLE = 1) AND (INVENTORY.VISIBLE = 1)) AND (INVENTORY.INVENTORYSTATE = 3)
);

有没有办法让它在 Oracle 上运行,或者使用带有 SELECT 语句的 IN 的更新在概念上是错误的?

在Oracle中,IN也可以,只需将Oracle中的EXCEPT改成MINUS即可 http://www.techonthenet.com/sql/update.php

http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10007.htm

http://docs.oracle.com/cd/E17952_01/refman-5.0-en/exists-and-not-exists-subqueries.html

此示例将是

UPDATE DELIVERY
SET VISIBLE = 0
WHERE DELIVERY.ID
IN 
(
    (SELECT DELIVERY.ID FROM delivery WHERE DELIVERY.VISIBLE = 1)
    MINUS
    (SELECT DELIVERY.ID 
    FROM delivery LEFT JOIN inventory ON INVENTORY.DELIVERYID = DELIVERY.ID
    WHERE (((DELIVERY.VISIBLE = 1) AND (INVENTORY.VISIBLE = 1)) 
          AND (INVENTORY.INVENTORYSTATE = 3)))
);

我认为您可以修改此查询,使其在 SQL 服务器 Oracle 中均有效:

UPDATE delivery d
   SET d.visible = 0
 WHERE d.visible = 1
   AND NOT EXISTS ( SELECT 1 FROM inventory i
                     WHERE i.deliveryid = d.id
                       AND i.visible = 1
                       AND i.inventorystate = 3 )

这将在 visible = 1 的任何地方更新 DELIVERY 设置 visible = 0 但在 INVENTORY 中没有对应的记录 visible = 1 并且inventorystate = 3.