• 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/