在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中文網其他相關文章!