删除前从另一个表中删除 SQL Merge 语句

问题描述

我有一个表:StudentHome,它对学生表有 fk 约束。所以当学生被删除时,我还需要从 StudentHome 中删除记录。当我使用纯删除语句时,我写了

Insert into Student 
select studentId,(other fields) from #Student

Delete sh
from StudenHome sh
left join #Student temps on temps.StudentId = sh.fkStudentId
where temps.studentId is null

Delete s
from Student s
left join #Student temps on temps.StudentId = s.studentId
where temps.studentId is null

但是,当我尝试使用merge语句时,在从student表中删除之前,我不确定如何从studenthome中删除记录。

merge into Student s
using #Student temps
on s.StudentID = temps.StudentId
when not matched by target then
insert ...
when not matched by source then

-- Delete from StudentHome 
delete; 

有没有办法实现上面的sudo代码,在我从student表中删除之前从表中删除:StudentHome?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)