如何正确使用 "with" 命令
How can I use the "with" command correctly
我有一个查询并且有效,通过它我得到了类别子集。
with cte as (
Select nid
from TB_Category
where nid = 429
union ALL
select ca.nid
from cte ct
inner join TB_Category ca on ca.parentid = ct.nid
)
SELECT p.nid
from TB_Category p
inner join cte ct on ct.nid = p.nid
要删除所选类别,我使用了以下命令,但出现错误:
DELETE FROM TB_Category
WHERE nid in (
with cte as (
Select nid
from TB_Category
where nid = 429
union ALL
select ca.nid
from cte ct
inner join TB_Category ca on ca.parentid = ct.nid
)
SELECT p.nid
from TB_Category p
inner join cte ct on ct.nid = p.nid
)
显示以下错误:
Incorrect syntax near the keyword 'WHERE'.
Incorrect syntax near the keyword 'with'. If this statement is a
common table expression, an xmlnamespaces clause or a change tracking
context clause, the previous statement must be terminated with a
semicolon.
Incorrect syntax near the keyword 'from'.
Incorrect syntax near the keyword 'with'.
Incorrect syntax near the keyword 'with'. If this statement is a
common table expression, an xmlnamespaces clause or a change tracking
context clause, the previous statement must be terminated with a
semicolon.
Incorrect syntax near ')'.
在查询开始时使用 CTE,如下所示:
with cte as (
Select nid
from TB_Category
where nid = 429
union ALL
select ca.nid
from cte ct
inner join TB_Category ca on ca.parentid = ct.nid
)
DELETE
FROM TB_Category
WHERE nid in (
SELECT p.nid
from TB_Category p
inner join cte ct on ct.nid = p.nid
)
您可以使用连接删除:
WITH cte AS (
SELECT nid
FROM TB_Category
WHERE nid= 429
UNION ALL
SELECT ca.nid
FROM cte ct
INNER JOIN TB_Category ca ON ca.parentid = ct.nid
)
DELETE t
FROM TB_Category t
INNER JOIN cte c ON c.nid = t.nid
但请注意,如果您在 nid 和 parentid 之间有一个真正的约束,删除将不会那么简单 - 如果删除尝试在子记录之前处理父记录,您可能会遇到约束冲突。
我有一个查询并且有效,通过它我得到了类别子集。
with cte as (
Select nid
from TB_Category
where nid = 429
union ALL
select ca.nid
from cte ct
inner join TB_Category ca on ca.parentid = ct.nid
)
SELECT p.nid
from TB_Category p
inner join cte ct on ct.nid = p.nid
要删除所选类别,我使用了以下命令,但出现错误:
DELETE FROM TB_Category
WHERE nid in (
with cte as (
Select nid
from TB_Category
where nid = 429
union ALL
select ca.nid
from cte ct
inner join TB_Category ca on ca.parentid = ct.nid
)
SELECT p.nid
from TB_Category p
inner join cte ct on ct.nid = p.nid
)
显示以下错误:
Incorrect syntax near the keyword 'WHERE'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Incorrect syntax near the keyword 'from'.
Incorrect syntax near the keyword 'with'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Incorrect syntax near ')'.
在查询开始时使用 CTE,如下所示:
with cte as (
Select nid
from TB_Category
where nid = 429
union ALL
select ca.nid
from cte ct
inner join TB_Category ca on ca.parentid = ct.nid
)
DELETE
FROM TB_Category
WHERE nid in (
SELECT p.nid
from TB_Category p
inner join cte ct on ct.nid = p.nid
)
您可以使用连接删除:
WITH cte AS (
SELECT nid
FROM TB_Category
WHERE nid= 429
UNION ALL
SELECT ca.nid
FROM cte ct
INNER JOIN TB_Category ca ON ca.parentid = ct.nid
)
DELETE t
FROM TB_Category t
INNER JOIN cte c ON c.nid = t.nid
但请注意,如果您在 nid 和 parentid 之间有一个真正的约束,删除将不会那么简单 - 如果删除尝试在子记录之前处理父记录,您可能会遇到约束冲突。