begin transaction;
declare @deleted_emails table (email nvarchar(100));
-- or whatever datatype/length you use
delete from table1
output deleted.email into @deleted_emails
where exists (select 1 from table2 t2 where t2.email = table1.email)
delete from table2
where exists (select 1 from @deleted_emails d where d.email = table2.email);
commit transaction;
1条答案
按热度按时间ikfrs5lh1#
sql server不支持多表删除,因此需要两个
delete
声明(如larnu所评论)。但这仍然有点棘手,因为你需要跟踪第一个声明中删除的电子邮件,以便在第二个声明中使用它们。我推荐一个表变量,您可以用
output
条款:将代码 Package 到
transaction
使其原子化(即,两个语句所做的更改一起提交或回滚)。