List of Users that Accessed a Table

  • I'm trying to find out what tables are being used in a Database.

    I don't want the last User but the User and the Dates.

    I have a script that return the last user but that is not going to work.

    Any input would be greatly appreciated.

    Thanks.

    The following script returns the last user but not all users and the Login Name:

    ITH LastActivity (ObjectID, LastAction) AS

    (

    SELECT object_id AS TableName,

    last_user_seek as LastAction

    FROM sys.dm_db_index_usage_stats u

    WHERE database_id = db_id(db_name())

    UNION

    SELECT object_id AS TableName,

    last_user_scan as LastAction

    FROM sys.dm_db_index_usage_stats u

    WHERE database_id = db_id(db_name())

    UNION

    SELECT object_id AS TableName,

    last_user_lookup as LastAction

    FROM sys.dm_db_index_usage_stats u

    WHERE database_id = db_id(db_name())

    )

    SELECT OBJECT_NAME(so.object_id) AS TableName,

    MAX(la.LastAction) as LastSelect

    FROM sys.objects so

    LEFT

    JOIN LastActivity la

    on so.object_id = la.ObjectID

    WHERE so.type = 'U'

    AND so.object_id > 100

    GROUP BY OBJECT_NAME(so.object_id)

    ORDER BY OBJECT_NAME(so.object_id)

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • you must have a trace or extended event in place,.

    SQL does not capture who ran select statements /users info unless you create something to capture it.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (7/22/2015)


    you must have a trace or extended event in place,.

    SQL does not capture who ran select statements /users info unless you create something to capture it.

    Hmm. I thought that I had a script that did that.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply