在 SQL Server 中使用 INNER JOIN 删除多个表
在 MySQL 中,使用 INNER JOIN 从多个表中删除可以通过以下语法完成:
DELETE t1,t2 FROM table1 AS t1 INNER JOIN table2 t2 ... INNER JOIN table3 t3 ...
但是,SQL Server 不支持此语法。相反,您可以利用“deleted”伪表来实现类似的结果:
begin transaction; declare @deletedIds table ( id int ); delete from t1 output deleted.id into @deletedIds from table1 as t1 inner join table2 as t2 on t2.id = t1.id inner join table3 as t3 on t3.id = t2.id; delete from t2 from table2 as t2 inner join @deletedIds as d on d.id = t2.id; delete from t3 from table3 as t3 ... commit transaction;
通过利用“deleted.id”伪表,您可以对相关表进行级联删除,确保数据诚信。
替代方案方法:
最终,最适当的方法取决于您系统的具体要求。
以上是如何在 SQL Server 中使用 INNER JOIN 从多个表中删除?的详细内容。更多信息请关注PHP中文网其他相关文章!