SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Track username and time of action on delete for rows


Track username and time of action on delete for rows

Author
Message
DataAnalyst011
DataAnalyst011
SSC Eights!
SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)

Group: General Forum Members
Points: 861 Visits: 456
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.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64607 Visits: 17978
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 Modens 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)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (232K reputation)SSC Guru (232K reputation)SSC Guru (232K reputation)SSC Guru (232K reputation)SSC Guru (232K reputation)SSC Guru (232K reputation)SSC Guru (232K reputation)SSC Guru (232K reputation)

Group: General Forum Members
Points: 232050 Visits: 46354
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, MVP, M.Sc (Comp Sci)
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


Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64607 Visits: 17978
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 Modens 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)
DataAnalyst011
DataAnalyst011
SSC Eights!
SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)

Group: General Forum Members
Points: 861 Visits: 456
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!
RandomEvent
RandomEvent
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1553 Visits: 528
Have you looked at the possibility of using CDC for some of this ?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64607 Visits: 17978
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. :-D

_______________________________________________________________

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 Modens 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)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64607 Visits: 17978
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 Modens 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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search