Is it possible to delete from multiple tables in the same SQL statement?

后端 未结 5 1497
自闭症患者
自闭症患者 2020-11-27 07:10

It\'s possible to delete using join statements to qualify the set to be deleted, such as the following:

DELETE J
FROM Users U
inner join LinkingTable J on U.         


        
5条回答
  •  情歌与酒
    2020-11-27 07:51

    The way you say is Possible in MY SQL but not for SQL SERVER

    You can use of the "deleted" pseudo table for deleting the values from Two Tables at a time like,

     begin transaction;
    
     declare @deletedIds table ( samcol1 varchar(25) );
    
     delete #temp1
     output deleted.samcol1 into @deletedIds
     from #temp1 t1
     join #temp2 t2
     on t2.samcol1 = t1.samcol1
    
     delete #temp2
     from #temp2 t2
     join @deletedIds d
     on d.samcol1 = t2.samcol1;
    
     commit transaction;
    

    For brief Explanation you can take a look at this Link

    and to Know the Use of Deleted Table you can follow this Using the inserted and deleted Tables

提交回复
热议问题