2
只是有一個SQL語句的奇怪問題。我正在使用WITH
語句爲樹結構收集父行和子行。SQL WITH CTE然後刪除問題
然後我想刪除一個家長,只有他們的孩子。
我的代碼如下(在存儲過程):
DECLARE @AssignmentIds TABLE
(
AssignmentIds int
);
with DeleteTree(AssignmentID)
as
(
select AssignmentID
from Assignment
where AssignmentID = @AssignmentID
union all
select a.AssignmentID
from Assignment a
join DeleteTree d on a.ParentID = d.AssignmentID
)
/* insert results of above with into table var*/
insert into @AssignmentIds
select AssignmentID from DeleteTree
/*This select returns the CORRECT Ids!*/
/*SELECT * from @AssignmentIds*/
/*Delete Roster Info*/
delete from [dbo].[Roster]
where [AssignmentId] in (select AssignmentId from @AssignmentIds)
/*Delete Assignments*/
delete from [dbo].[Assignment]
where [AssignmentID] in (select AssignmentID from @AssignmentIds)
怪異的一部分是,如果我運行的結果有一個選擇,我得到正確的正確分配ID列表。
但是,當我運行delete
語句時,它將刪除assignments表中的所有行,而不是僅刪除with語句中的Ids!
嗨Giorgi,是的,我檢查,並沒有在名單表或分配表上的級聯刪除 - 感謝評論。 – user1191559