Erase rows in multiple tables

-1

I'm trying to delete information from 2 tables simultaneously that are linked to each other but I'm not getting it, give me this error: Anybody help me please?

    
asked by anonymous 30.06.2016 / 20:44

1 answer

1

As you have not specified the database, I am responding in SQL SERVER, and tables in my database;

You can take advantage of the "deleted id" of the table something like:

begin transaction;

   declare @deletedIds table ( id int );

   delete e
   output p.IdPessoa into @deletedIds
   from Pessoas p
    join Estados e
    on e.IdPessoa = p.IdPessoa

   delete P
   from Pessoas P
    join @deletedIds d
      on d.id = P.IdPessoa;

commit transaction;

You can get the id of the table with the "output" notice that the table that is the first to be deleted is the one that has dependency relation in this case (States).

    
01.07.2016 / 19:37