SQL server Delete script order -
my tables have following relations

as can see firstentity can have multiple transactions related record. transaction divided 2 tables because represents inheritance hierarchy (table per type in entity framework).
i need create script delete record firstentitytransaction , transaction given firstentityid. delete should follow following order:
- delete records
firstentitytransaction - delete records
transaction - delete record
firstentity
the problem when execute first delete (firstentitytransaction) not have way find related transactions transactionid. there way can save ids , execute second delete?
edit: have modified post have more meaningful diagram
below example inserts deleted entity transactions table variable, subsequently used delete transaction rows.
declare @deletedmyentitytransaction table ( transactionid int ); delete dbo.myentitytransaction output deleted.transactionid @deletedmyentitytransaction myentityid = @myentityid; delete dbo.[transaction] transactionid in ( select transactionid @deletedmyentitytransaction ); delete dbo.myentity myentityid = @myentityid;
Comments
Post a Comment