0

Hello we have recently learned how to create triggers and I have to do one on delete from Users, which also deletes said user from referenced tables but I don't know how to make it in SQL server (we had to choose a DB Management system and I chose sql server) but I recently learned you can't do triggers before the action so I don't know how to solve this, I had something like

create trigger myTrigger
on User
for delete
AS
delete from GroupParticipants where participantId in (select deleted.codUser from Deleted)
delete from (...) where column in (select deleted.codUser from Deleted)
GO

Yet, as this happens after the delete, said delete never occurs due to referential integrity, how do I solve this seemingly simple problem?

My tables are something like this:

User (codUser, ...)

GroupParticipant (groupId, codUser)

OtherTable(codUser)

and other irrelevant tables

Community
  • 1
  • 1
Lucas Araujo
  • 133
  • 1
  • 2
  • 11
  • Have you looked at an [instead of delete](https://technet.microsoft.com/en-us/library/ms191208%28v=sql.105%29.aspx?f=255&MSPPError=-2147217396) trigger? – HABO Jun 18 '16 at 19:25

1 Answers1

1

You may want to re-do the constraints to use cascade delete (this way, you won't need a trigger): See: https://stackoverflow.com/a/6260736/3394342

Community
  • 1
  • 1
Frank Alvaro
  • 468
  • 4
  • 12
  • I don't get it if this happens after delete how will I be able to delete the user on the first place if sql management will give me an integrity error before the trigger even triggers – Lucas Araujo Jun 18 '16 at 19:12