CREATE TRIGGER [dbo].[C1_Deletions] ON [dbo].[C1] INSTEAD OF DELETE AS
SET NOCOUNT ON
IF EXISTS ( SELECT 'True' FROM deleted JOIN C1 ON deleted.ACCOUNTNO = C1.ACCOUNTNO )
BEGIN
INSERT INTO [GDeletions].[dbo].[C1] SELECT * FROM deleted
DELETE C1 FROM C1 INNER JOIN DELETED ON C1.ACCOUNTNO = DELETED.ACCOUNTNO
END
So that is the trigger i’m trying to use, it works well when i’m deleting by accountno, but when i need to delete by recid(another column) i’m unable to.
If i change the INSTEAD OF to AFTER, i get errors about ntext, image columns not being allowed. Is there any way around this issue? I cannot be the one specifying the deletion string, the program itself does that i just need the trigger to grab the data that is being deleted.
The bigger problem i have is another table that stores history, it stored it with the accountno matching to the c1 table but then there is also recid which is unique to every entry. If i go to delete a C1 entry, it deletes all from history using accountno, but if i delete a single history entry then it deletes by recid.
I found out that, even if you delete multiple rows at once, each row is still put into the DELETED temporary table during the query execution, so my trigger then goes through each record and matches on recid(which is unique to each row) and deletes/moves from there.