August 26, 2013 at 12:53 pm
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.
August 26, 2013 at 2:12 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 26, 2013 at 2:21 pm
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
August 26, 2013 at 2:44 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 27, 2013 at 7:08 am
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!
August 27, 2013 at 7:27 am
Have you looked at the possibility of using CDC for some of this ?
August 27, 2013 at 7:31 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 27, 2013 at 7:32 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply