Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete trigger issue with ON DELETE CASCADE in sql server

I have two Tables in Sql server

Table A: Id UNIQUEIDENTIFIER NOT NULL
Table B: Id UNIQUEIDENTIFIER NOT NULL
         AId UNIQUEIDENTIFIER NOT NULL,ON DELETE CASCADE (This is Id of Table A)

(Notice B has column AId that references A with ON DELETE CASCADE option). B also has a delete trigger like

CREATETRIGGER B_Delete_Trigger
ON [dbo].[B]
FOR DELETE
AS
SELECT * FROM DELETED AS B INNER JOIN A ON B.AId=A.Id

Now when I perform delete operation on Table B, above trigger returns rows, but when on Table A nothing is returned.

As much I know SQL Server first delete records from Child Table(B in this case) and then from parent(A in this case), so above trigger should return some record in both cases.

Please, tell me how to access parents records, Or my observation is wrong.

like image 575
user369182 Avatar asked Dec 10 '25 19:12

user369182


1 Answers

Actually when you call this trigger on table A. The execution sequence is

  1. DELETE Record from child table B.
  2. DELETE Record from Parent table A.
  3. Trigger call

And SELECT * FROM DELETED contains only data from A table.

like image 66
Habib Avatar answered Dec 13 '25 19:12

Habib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!