本文介绍了如何通过传递单个变量从两个表中删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码通过传递单个值来删除两个表中的行但是我得到的错误就像



'',''附近的语法不正确>


i used below code to delete rows from two table by passing single value but i am getting error like

Incorrect syntax near '',''

DELETE messages ,usersmessages FROM messages INNER JOIN usersmessages
WHERE messages.messageid= usersmessages.messageid and messages.messageid = 1

推荐答案

BEGIN TRY  
  BEGIN TRANSACTION  

   DELETE FROM usersmessages
   WHERE  messageid = 1

   DELETE FROM messages 
   WHERE  messageid = 1


  COMMIT TRANSACTION  
 END TRY  
 BEGIN CATCH  
  ROLLBACK TRANSACTION  
 END CATCH  


begin transaction;

   declare @deletedIds table ( id int );

   delete t1
   output deleted.id into @deletedIds;
   from table1 t1
    join table2 t2
      on t2.id = t1.id
    join table3 t3
      on t3.id = t2.id;

   delete t2
   from table2 t2
    join @deletedIds d
      on d.id = t2.id;

   delete t3
   from table3 t3 ...

commit transaction;





快乐编码:)



Happy Coding :)


DELETE FROM table_name
WHERE some_column=some_value


这篇关于如何通过传递单个变量从两个表中删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 06:55