我可以在多个 DML 语句中引用一个 Sqlite CTE 吗?

Can I reference a Sqlite CTE in more than one DML statement?

在一条语句中,我想更新一组行并删除另一组,两者都基于某些 CTE。然而,当我执行它时,第二个 DML,即 DELETE,抱怨说 CTE 不再可用。这在 Sqlite 中是不可能的吗?

我查看了 WITH reference for Sqlite,但我唯一发现的是警告部分,那是关于复合语句的,这不是我在这里所做的。

这是我的 SQL,删除了 CTE 的内容,因为其中有一些专有内容,而且我认为这对问题无关紧要(但如果不正确请告诉我) :

with cteOldAboutCmds as (
  ...
)
, cteFrequency as (
  ...
)
, cteNewAboutCmdMaster as (
  ...
)

update cmds 
set freqrankalltime = freqrankalltime + (select fr from cteFrequency) 
where id = (select id from cteNewAboutCmdMaster);

delete 
from cmds
where id in (select id from cteOldAboutCmds);

我也尝试将上面的内容包装在 begin transaction;commit transaction; 中,但这没有帮助。

错误信息是:"no such table: cteOldAboutCmds".

CTE 是单个 SQL 语句的一部分,这几乎就是它的全部要点。

要使命名查询可用于多个语句,请使用 views。如果您不想影响其他连接,请使用临时视图。

我已经接受了 CL 的回答,但为了其他人的利益,我想格式化我最终使用的代码:

begin transaction;

create temp view OldAboutCmds as
select *
from...;

create temp view Frequency as
select...;

create temp view NewAboutCmdMaster as
select...;

update cmds 
set ...
where ...;

delete 
from cmds
where ...;

commit transaction;

drop view OldAboutCmds;
drop view Frequency;
drop view NewAboutCmdMaster;