Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Track username and time of action on delete for rows Expand / Collapse
Author
Message
Posted Monday, August 26, 2013 12:53 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 3:10 PM
Points: 92, Visits: 339
I've been trying on this one for a while. I implemented change data capture, but I found interpreting some of the columns difficult (even after reading through several articles online). Since I only need to track deletes on one or two tables, I decided to try an on delete sql trigger. Well, I'm having a hard time with this one too.

Here is what I would like to capture:
1. The row that was deleted
2. When the row was deleted
3. What user deleted the row

Here is some sample data for anyone that can lend a hand. Thanks for any help!

CREATE TABLE #attend
(
attendID int,
lname varchar(100),
fname varchar(100)
);

INSERT INTO #attend (attendID, lname, fname)
VALUES
(1, 'Smith', 'Joe'),
(2, 'Black', 'Sam'),
(3, 'Williams', 'Ralph');

EDIT: I guess I should note that specifically I'm having a hard time with capturing which user did the deleting.
Post #1488478
Posted Monday, August 26, 2013 2:12 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:00 PM
Points: 13,224, Visits: 12,068
DataAnalyst011 (8/26/2013)
I've been trying on this one for a while. I implemented change data capture, but I found interpreting some of the columns difficult (even after reading through several articles online). Since I only need to track deletes on one or two tables, I decided to try an on delete sql trigger. Well, I'm having a hard time with this one too.

Here is what I would like to capture:
1. The row that was deleted
2. When the row was deleted
3. What user deleted the row

Here is some sample data for anyone that can lend a hand. Thanks for any help!

CREATE TABLE #attend
(
attendID int,
lname varchar(100),
fname varchar(100)
);

INSERT INTO #attend (attendID, lname, fname)
VALUES
(1, 'Smith', 'Joe'),
(2, 'Black', 'Sam'),
(3, 'Williams', 'Ralph');

EDIT: I guess I should note that specifically I'm having a hard time with capturing which user did the deleting.


I don't get what this temp table has to do with the audit trigger you are trying to create.

I can help you figure out the details you said you wanted.


1. The row that was deleted


In the deleted table


2. When the row was deleted


getdate()


3. What user deleted the row


There are many ways to get the user. If this is an application and the connection is always made using the same sql user you are out of luck. If however you use windows authentication you might be able to use SUSER_SNAME().


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1488490
Posted Monday, August 26, 2013 2:21 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:38 AM
Points: 42,753, Visits: 35,841
Sean Lange (8/26/2013)
If however you use windows authentication you might be able to use SUSER_SNAME().


I would recommend ORIGINAL_LOGIN() instead, in case there's any impersonation occurring.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1488493
Posted Monday, August 26, 2013 2:44 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:00 PM
Points: 13,224, Visits: 12,068
GilaMonster (8/26/2013)
Sean Lange (8/26/2013)
If however you use windows authentication you might be able to use SUSER_SNAME().


I would recommend ORIGINAL_LOGIN() instead, in case there's any impersonation occurring.


Deja vu...I think you have told me that before Gail and I once again seem to have to just forgotten such excellent advice. Thanks for the reminder.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1488499
Posted Tuesday, August 27, 2013 7:08 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 3:10 PM
Points: 92, Visits: 339
Thanks to you both - I really appreciate the advice. It looks like I'm out of luck: the connection made always uses SQL Server. But this saved me a week of trying to figure out why only the system user showed in my query!

As a side note - the table script with data was just in case someone needed to demo something. I figure its always better to err on the side of including it.

Thanks again!
Post #1488741
Posted Tuesday, August 27, 2013 7:27 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Tuesday, June 10, 2014 4:44 AM
Points: 516, Visits: 160
Have you looked at the possibility of using CDC for some of this ?
Post #1488755
Posted Tuesday, August 27, 2013 7:31 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:00 PM
Points: 13,224, Visits: 12,068
DataAnalyst011 (8/27/2013)
Thanks to you both - I really appreciate the advice. It looks like I'm out of luck: the connection made always uses SQL Server. But this saved me a week of trying to figure out why only the system user showed in my query!

As a side note - the table script with data was just in case someone needed to demo something. I figure its always better to err on the side of including it.

Thanks again!


You're welcome. And nobody around here will ever complain about receiving ddl and sample data. I just couldn't figure out what it was for.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1488764
Posted Tuesday, August 27, 2013 7:32 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:00 PM
Points: 13,224, Visits: 12,068
Jason.Reeves (8/27/2013)
Have you looked at the possibility of using CDC for some of this ?


Guessing you missed it in the OP.


I implemented change data capture, but I found interpreting some of the columns difficult (even after reading through several articles online).


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1488765
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse