0

I want to delete data from 4 table using single query.

delete e1,e2,e3,e4 FROM sas.RolesInMenuOperations as e1 INNER JOIN
sas.RolesMenus as e2 ON e1.RoleMenuId = e2.Id INNER JOIN
sas.RolesInModules as e3 ON e2.RolesInModulesId = e3.Id INNER JOIN
sas.Roles as e4 ON e3.RoleId = e4.Id
where e4.Id=5

This is what I have done so far. I was refering this link Is it possible to delete data from 4 table using Joins. How to achieve this or I want to write 4 different queries?

Though this query works properly

delete e1 FROM sas.RolesInMenuOperations as e1 INNER JOIN
sas.RolesMenus as e2 ON e1.RoleMenuId = e2.Id INNER JOIN
sas.RolesInModules as e3 ON e2.RolesInModulesId = e3.Id INNER JOIN
sas.Roles as e4 ON e3.RoleId = e4.Id
where Roles.Id=5
3
  • this will work on MySQL but not on SQL Server. Commented Mar 25, 2013 at 7:21
  • Is any another way around for doing this? Commented Mar 25, 2013 at 7:22
  • @JW: Deleting from multiple tables in SQL Server would require multiple queries then, right? Commented Mar 25, 2013 at 7:25

2 Answers 2

2

You can not delete multiple table data using a single query in SQL Server. Best concept is to give FK in relative table and use ON DELETE CASCADE or use individual queries to delete from multiple tables.

Sign up to request clarification or add additional context in comments.

Comments

2

So you are looking for MS SQL syntax ? Something liek this works:

DELETE table 
FROM table a
INNER JOIN table_b b on b.id = a.id
WHERE [my filter condition]

So if you want to delete more tables rows you need to create more statements and fire them I think.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.