11

尝试这样做(在 SQL Server 中工作):

WITH X AS (), Y AS (), Z AS ()
DELETE FROM TBL
WHERE TBL.ID IN (SELECT ID FROM Z);

这适用于甲骨文:

WITH X AS (), Y AS (), Z AS ()
SELECT * FROM TBL
WHERE TBL.ID IN (SELECT ID FROM Z);

但 DELETE 没有:ORA-00928: missing SELECT 关键字

我的子查询相当大,是否有不同的语法可以让它工作?

4

3 回答 3

13

您不能将子查询分解/CTE 与 SELECT 语句一起使用。从文档中:

您可以在任何顶级 SELECT 语句和大多数类型的子查询中指定此子句。

你可以这样做:

DELETE FROM tbl WHERE tbl.id IN
(WITH X AS (), Y AS (), Z AS ()
SELECT id FROM TBL
 WHERE TBL.ID IN (SELECT ID FROM Z));
于 2011-07-06T21:37:41.587 回答
8

我得到了这个工作(我确定这在 SQL Server 中不起作用):

DELETE FROM TBL
WHERE TBL.ID IN (
    WITH X AS (), Y AS (), Z AS ()
    SELECT ID FROM Z
);
于 2011-07-06T21:43:43.927 回答
1

好吧,至少,您需要让所有别名查询以某种方式出现在 FROM 语句中。我不知道是否还有更多问题,但这是必须的(我相信 00928 是您不这样做时发生的错误)。

于 2011-07-06T21:30:34.863 回答