带有子查询因子的Oracle DELETE语句

时间:2011-07-06 21:22:18

标签: oracle oracle10g ora-00928 subquery-factoring

尝试执行此操作(适用于SQL Server):

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

这适用于Oracle:

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

但是DELETE没有:ORA-00928:缺少SELECT关键字

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

3 个答案:

答案 0 :(得分:12)

除了SELECT语句之外,您不能使用Subquery Factoring / CTE。 From the documentation:

  

您可以在任何中指定此子句   顶级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));

答案 1 :(得分:8)

我让它工作(我确信它在SQL Server中不起作用):

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

答案 2 :(得分:1)

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