Sql query to remove from two tables at once, SQL Server

I have two related tables, Ad_consult type and Ad_consult, a query type has multiple queries, I look for the sql script to remove both the type and queries that belong to that type.

DELETE Ad_TipoConsulta, Ad_Consultas FROM Ad_TipoConsulta 
JOIN Ad_Consultas ON Ad_Consultas.Sec_TipoConsulta = Ad_TipoConsulta.Sec_TipoConsulta
WHERE Ad_Consultas.Sec_TipoConsulta = 121;

This query generates error in the (,) separating Ad_consult type, Ad_consults.

 5
Author: Shaz, 2016-04-28

4 answers

To avoid such queries you should configure the relationship with the cascading deletion rule, so when you delete a query type the database engine will only take care of deleting the associated queries,

enter the description of the image here

In the image if and I place the cascading relationship, when I delete a workshop, all orders belonging to that workshop will be deleted.

I know it's not the answer you want since you're explicitly asking by query, but it is a valid path to take.

 5
Author: Bloodday, 2016-04-28 21:05:15

I know it's a little late and you've probably figured it out, anyway I'll answer if anyone else does:)

In SQL SERVER - is not possible delete from two tables at the same time using the sentence DELETE (I understand that in MySQL if you can), at least until SQL SERVER 2016: according to the documentation (https://msdn.microsoft.com/en-us/library/ms189835.aspx) you can only specify a table of destination for removal.

If you can't set up a cascading delete rule on the Foreign Key as bloodday indicates (I guess it has one), the only way I see would be to delete queries that have that type first and then the type itself, this in two separate operations:

DELETE Ad_Consultas 
WHERE Sec_TipoConsulta= 121;

DELETE Ad_TipoConsulta
WHERE Sec_TipoConsulta= 121;

And if there is a possibility that one of the deletions will fail, both must be executed on the same transaction (with proper error handling)to prevent it from being half done (e.g. delete queries, but not the type)

 4
Author: Roimer, 2016-08-11 02:04:45

If you want to remove from the two tables in the same query you should use a INNER JOIN , for example:

DELETE Ad_TipoConsulta, Ad_Consultas FROM Ad_TipoConsulta INNER JOIN Ad_Consultas 
 2
Author: Jorgesys, 2016-04-28 20:47:52

I think one way to do this is to first delete the table with the foreign key and then delete the normal table, for example something like this:

DELETE 
  FROM Ad_TipoConsulta 
WHERE Ad_TipoConsulta.Sec_TipoConsulta = 

(SELECT Ad_Consultas.Sec_TipoConsulta 
  FROM Ad_Consultas 
WHERE Ad_Consultas.Sec_TipoConsulta = 121 
--Aqui pueden ir más validaciones 
)

That would do is remove all rows of "Ad_consult" that have in their foreign key the value of the Select Ad_consults.Sec_consult type

Then you delete Ad_consults if it is also what you want, doing something like

DELETE 
       FROM Ad_Consultas
WHERE Ad_Consultas.Sec_TipoConsulta = 121

I work something similar when the SELECT it brought only one row, I don't know if it doesn't work if it's more than one

 0
Author: Luis Daniel, 2020-11-17 00:37:48