Suppose I have a table called Companies that has a DepartmentID column. There’s also a Departaments table that has as EmployeeID column. Of course I have an Employee table as well. The problem is that I want to delete a company, so first i have to delete all the employees for every departament and then all the departaments in the company. Cascade Delete is not an option, therefore i wish to use nested transactions. I’m new to SQL so I would appreciate your help.
Suppose I have a table called Companies that has a DepartmentID column. There’s also
Share
I’m not sure why you need nested transactions here. You only need one actual transaction:
Note the double FROM, that is not a typo, it’s the correct SQL syntax for performing a JOIN in a DELETE.
Each statement is atomic, either the entire DELETE will succeed or fail, which isn’t that important in this case because the entire batch will either succeed or fail.
BTW- I think you had your relationships backwards. The Department would not have an EmployeeID, the Employee would have a DepartmentID.