-
Notifications
You must be signed in to change notification settings - Fork 84
/
Query an audit file.sql
54 lines (53 loc) · 1.95 KB
/
Query an audit file.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Query an audit file
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
-- This script returns all relevant columns from a database audit for a nominated login and date/time range
-- Values to be modified before execution:
DECLARE @DBName VARCHAR(100) = '<DatabaseName>';
DECLARE @AuditPath VARCHAR(100) = '<PathToAuditsFolder>';
DECLARE @LoginName NVARCHAR(128) = N'<LoginName>';
DECLARE @StartDateTime DATETIME2(7) = '<yyyy-mm-dd hh:mm>';
DECLARE @EndDateTime DATETIME2(7) = '<yyyy-mm-dd hh:mm>';
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, GETUTCDATE(), CURRENT_TIMESTAMP), event_time) AS event_time_local,
sequence_number,
action_id,
succeeded,
permission_bitmask,
is_column_permission,
session_id,
server_principal_id,
database_principal_id,
target_server_principal_id,
target_database_principal_id,
object_id,
class_type,
session_server_principal_name,
server_principal_name,
server_principal_sid,
database_principal_name,
target_server_principal_name,
target_server_principal_sid,
target_database_principal_name,
server_instance_name,
database_name,
schema_name,
object_name,
statement,
additional_information,
file_name,
audit_file_offset,
user_defined_event_id,
user_defined_information,
audit_schema_version,
sequence_group_id,
transaction_id,
client_ip,
application_name,
duration_milliseconds,
response_rows,
affected_rows
FROM sys.fn_get_audit_file(@AuditPath + '\' + @DBName + '\*', DEFAULT, DEFAULT)
WHERE session_server_principal_name = @LoginName
AND DATEADD(MINUTE, DATEDIFF(MINUTE, GETUTCDATE(), CURRENT_TIMESTAMP), event_time)
BETWEEN @StartDateTime AND @EndDateTime
ORDER BY DATEADD(MINUTE, DATEDIFF(MINUTE, GETUTCDATE(), CURRENT_TIMESTAMP), event_time) ASC;
GO