Can we get a Audit report where statements executed by user and from which machine for a database

i know we have Find SQL/Trends where we do get these details but not in a comprehensive manner.

What i know we do get is focused results w.r.t machines/users/programs. Then drill down to collate all these details for any particular statement.

Ultimately, i need something like Realtime sessions format kind of report.

Not sure how much of effort that is and how to come up with a query as the table names and column names are wierd in DPA repo

  • After digging more, i came up with below and works for me!

    DECLARE @monitorID INT;
    DECLARE @FROM_DATE DATETIME;
    DECLARE @TO_DATE DATETIME;
    DECLARE @SQL NVARCHAR(MAX);
    DECLARE @User NVARCHAR(100);
    DECLARE @Keyword NVARCHAR(100);
    DECLARE @ClientName NVARCHAR(100);
    
    -- Set the monitor ID and date range
    SET @monitorID =  --id of server;
    SET @FROM_DATE = GETDATE() - 7; --Last 7 days
    SET @TO_DATE = GETDATE();
    SET @User = NULL; -- Replace with the 'specific_user' or set to NULL for all users
    SET @Keyword = NULL; -- Replace with the 'keyword' or set to NULL for all keywords
    SET @ClientName = NULL; -- Replace with the 'client_name' or set to NULL for all client names
    
    -- Construct the dynamic SQL query
    SET @SQL = 
    'SELECT DISTINCT ' +
    'MD.NAME [Database/Name], ' +
    'MM.Name [Application/client name], ' +
    'ST.ST [Statement], ' +
    'U.Name [User], ' +
    'SW.D [Execution Time] ' +
    'FROM dbo.CONSW_' + CAST(@monitorID AS NVARCHAR) + ' SW ' +
    'LEFT OUTER JOIN dbo.CON_SQL_NAME N ON SW.IZHO = N.HASH ' +
    'LEFT OUTER JOIN dbo.CONST_' + CAST(@monitorID AS NVARCHAR) + ' ST ON SW.IZHO = ST.H ' +
    'LEFT OUTER JOIN dbo.CONO_' + CAST(@monitorID AS NVARCHAR) + ' MD ON SW.IXOY = MD.ID ' +
    'LEFT OUTER JOIN dbo.CONM_' + CAST(@monitorID AS NVARCHAR) + ' MM ON SW.PWMY = MM.ID ' +
    'INNER JOIN dbo.CONU_' + CAST(@monitorID AS NVARCHAR) + ' U ON SW.XCUW = U.ID ' +
    'WHERE SW.D BETWEEN ' +
    'CONVERT(DATETIME, ''' + CONVERT(NVARCHAR, @FROM_DATE, 101) + ''', 101) AND ' +
    'CONVERT(DATETIME, ''' + CONVERT(NVARCHAR, @TO_DATE, 101) + ''', 101) ' +
    'AND (' + ISNULL('U.Name = ''' + @User + '''', '1=1') + ') ' +
    'AND (' + ISNULL('ST.ST LIKE ''%' + @Keyword + '%''', '1=1') + ') ' +
    'AND (' + ISNULL('MM.Name = ''' + @ClientName + '''', '1=1') + ') ' +
    'GROUP BY MD.NAME, MM.Name, ST.ST, U.Name, SW.D ' +
    'ORDER BY MD.NAME';
    
    -- Execute the dynamic SQL
    EXEC sp_executesql @SQL;

  • Check out this custom query in the Content Exchange are for DPA:  Top SQL by Any Dimension 

    This one only uses a single dimension in the WHERE clause, but you can use this to create your own. And I see you did that and your query looks great as well!

  • Yes, this custom query looks super useful!