Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

after delete trigger

Status
Not open for further replies.

sparkbyte

Technical User
Sep 20, 2002
879
US
Does enyone have an example of an after delete trigger that copies the deleted row(s) into an archive table and adds who deleted it and when??

I have an activity table (schema.Activity) and the archive table (Archive.Activity).


Thanks

John Fuhrman
 
Here is one we use.. I'm not dure about the before and after stuff.

Code:
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE TRIGGER [dbo].[tr_Object_d] ON [dbo].[OBJECT] 
FOR DELETE
AS


IF @@ROWCOUNT =0
RETURN

insert objecthist(OBJECT_ID,OBJECT_CLASS_ID,NAME,SERVER,RELATIVE_PATH,IS_LOCKED,LOCKED_BY,
		HAS_INDEXES,IS_IMAGE,IS_CHARACTER,IS_FULL_TEXT_INDEXED,BATCH_NAME,CREATE_TIME,Indexed_By,Scanned_By,
		DateChanged,ChangedBy,Change,INDEXED_TIME,fkImageFax,OverrideTasking)

select OBJECT_ID,OBJECT_CLASS_ID,NAME,SERVER,RELATIVE_PATH,IS_LOCKED,LOCKED_BY,HAS_INDEXES,IS_IMAGE,
	IS_CHARACTER,IS_FULL_TEXT_INDEXED,BATCH_NAME,CREATE_TIME,Indexed_By,Scanned_By ,getdate(),user_name(),'D',INDEXED_TIME,fkImageFax,OverrideTasking
from deleted 


GO
 
the same code only after delete will work

SQL:
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE TRIGGER [dbo].[tr_Object_d] ON [dbo].[OBJECT] 
AFTER DELETE
AS


IF @@ROWCOUNT =0
RETURN

insert objecthist(OBJECT_ID,OBJECT_CLASS_ID,NAME,SERVER,RELATIVE_PATH,IS_LOCKED,LOCKED_BY,
		HAS_INDEXES,IS_IMAGE,IS_CHARACTER,IS_FULL_TEXT_INDEXED,BATCH_NAME,CREATE_TIME,Indexed_By,Scanned_By,
		DateChanged,ChangedBy,Change,INDEXED_TIME,fkImageFax,OverrideTasking)

select OBJECT_ID,OBJECT_CLASS_ID,NAME,SERVER,RELATIVE_PATH,IS_LOCKED,LOCKED_BY,HAS_INDEXES,IS_IMAGE,
	IS_CHARACTER,IS_FULL_TEXT_INDEXED,BATCH_NAME,CREATE_TIME,Indexed_By,Scanned_By ,getdate(),user_name(),'D',INDEXED_TIME,fkImageFax,OverrideTasking
from deleted 


GO
 
Thanks,

altering it to a FOR Delete seems to have got it.

Also, thanks for the reminder to test for empty set!!

Thanks

John Fuhrman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top