October 14, 2010 at 12:43 pm
Hi,
We are having an issue with some process deleting records from a table (SS 2005). I need to put a trigger in place that will delete the data from the original table, save the record into a new table and include the user who deleted the record.
Any help would be appreciated.
TIA
October 14, 2010 at 12:47 pm
Post the definitions of the tables please.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 14, 2010 at 12:51 pm
Source table: dbo.[HCS_STG_CNSMR_APLCTN]
[STG_DATA_PRCSS_TMSTMP] [datetime] NULL,
[STG_DATA_ROW_ID] [int] NOT NULL,
[CNSMR_APLCTN_CD] [char](3) NULL,
[PRCSS_STATUS_CD] [char](3)
Target table:
[dbo].[deleted_HCS_STG_CNSMR_APLCTN]
[STG_DATA_PRCSS_TMSTMP] [datetime] NULL,
[STG_DATA_ROW_ID] [int] NOT NULL,
[CNSMR_APLCTN_CD] [char](3) NULL,
[PRCSS_STATUS_CD] [char](3),
[USERID]varchar(8)
October 14, 2010 at 12:58 pm
Should the user column be set to the login name of the user who deleted the rows?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 14, 2010 at 1:05 pm
yes, that is correct.
October 14, 2010 at 1:30 pm
Edit: nevermind. I'm not thinking
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 14, 2010 at 1:38 pm
This should do the job. Note that I've increased the size of the userID column and added a datedeleted. Doesn't help much if you know who deleted rows but not when they were deleted.
CREATE TABLE [dbo].[deleted_HCS_STG_CNSMR_APLCTN] (
[STG_DATA_PRCSS_TMSTMP] [datetime] NULL,
[STG_DATA_ROW_ID] [int] NOT NULL,
[CNSMR_APLCTN_CD] [char](3) NULL,
[PRCSS_STATUS_CD] [char](3),
[USERID] varchar(255),
[DateDeleted] DATETIME DEFAULT GETDATE()
)
GO
CREATE TRIGGER AuditDeletedRows ON dbo.[HCS_STG_CNSMR_APLCTN]
AFTER DELETE
AS
INSERT INTO [dbo].[deleted_HCS_STG_CNSMR_APLCTN] (STG_DATA_PRCSS_TMSTMP, STG_DATA_ROW_ID, CNSMR_APLCTN_CD, PRCSS_STATUS_CD, USERID)
SELECT STG_DATA_PRCSS_TMSTMP, STG_DATA_ROW_ID, CNSMR_APLCTN_CD, PRCSS_STATUS_CD, ORIGINAL_LOGIN() FROM deleted
GO
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 14, 2010 at 1:43 pm
You are the KING! Thank you!
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply