You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/relational-databases/system-functions/sys-fn-get-audit-file-v2-transact-sql.md
+67-31Lines changed: 67 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
---
2
2
title: "sys.fn_get_audit_file_v2 (Transact-SQL)"
3
3
description: "The sys.fn_get_audit_file_v2 system function replaces sys.fn_get_audit_file, and returns information from an audit file created by a server audit in SQL Server."
The `sys.fn_get_audit_file_v2` system function in [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)] is designed to retrieve audit log data with enhanced efficiency compared to its predecessor, `sys.fn_get_audit_file`. The function introduces time-based filtering at both the file and record levels, providing significant performance improvements, particularly for queries targeting specific time ranges.
28
-
29
-
> [!IMPORTANT]
30
-
> `sys.fn_get_audit_file_v2` is currently supported on [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)] only.
27
+
The `sys.fn_get_audit_file_v2` system function is designed to retrieve audit log data with enhanced efficiency compared to its predecessor, `sys.fn_get_audit_file`. The function introduces time-based filtering at both the file and record levels, providing significant performance improvements, particularly for queries targeting specific time ranges.
31
28
32
29
Returns information from an audit file created by a server audit in [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)]. For more information, see [SQL Server Audit (Database Engine)](../security/auditing/sql-server-audit-database-engine.md).
33
30
31
+
For information on setting up Azure SQL Database auditing, see [Get Started with SQL Database auditing](/azure/sql-database/sql-database-auditing?view=azuresql-db&preserve-view=true).
32
+
33
+
For information on setting up Fabric Data Warehouse auditing, see [SQL Audit Logs in Fabric Data Warehouse](/fabric/data-warehouse/sql-audit-logs).
**Applies to:**[!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)] only
53
+
50
54
Specifies the directory or path and file name for the audit file set to be read. *file_pattern* is **nvarchar(260)**.
51
55
52
56
Passing a path without a file name pattern generates an error.
@@ -59,12 +63,16 @@ This argument is used to specify a blob URL (including the storage endpoint and
59
63
60
64
#### *initial_file_name*
61
65
66
+
**Applies to:**[!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)] only
67
+
62
68
Specifies the path and name of a specific file in the audit file set to start reading audit records from. *initial_file_name* is **nvarchar(260)**.
63
69
64
70
The *initial_file_name* argument must contain valid entries or must contain either the `default` or `NULL` value.
65
71
66
72
#### *audit_record_offset*
67
73
74
+
**Applies to:**[!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)] only
75
+
68
76
Specifies a known location with the file specified for the *initial_file_name*. When this argument is used, the function starts reading at the first record of the buffer immediately following the specified offset.
69
77
70
78
The *audit_record_offset* argument must contain valid entries or must contain either the `default` or `NULL` value. *audit_record_offset* is **bigint**.
@@ -110,28 +118,28 @@ The following table describes the audit file content returned by this function.
110
118
|`statement`|**nvarchar(4000)**| Transact-SQL statement if it exists. Nullable. Returns `NULL` if not applicable. |
111
119
|`additional_information`|**nvarchar(4000)**| Unique information that only applies to a single event is returned as XML. A few auditable actions contain this kind of information.<br /><br />One level of T-SQL stack is displayed in XML format for actions that have T-SQL stack associated with them. The XML format is: `<tsql_stack><frame nest_level = '%u' database_name = '%.*s' schema_name = '%.*s' object_name = '%.*s' /></tsql_stack>`<br /><br />`frame nest_level` indicates the current nesting level of the frame. The module name is represented in three part format (`database_name`, `schema_name`, and `object_name`). The module name is parsed to escape invalid XML characters like `<`, `>`, `/`, `_x`. They're escaped as `_xHHHH_`. The `HHHH` stands for the four-digit hexadecimal UCS-2 code for the character. Nullable. Returns `NULL` when there's no additional information reported by the event. |
112
120
|`file_name`|**varchar(260)**| The path and name of the audit log file that the record came from. Not nullable. |
113
-
|`audit_file_offset`|**bigint**| The buffer offset in the file that contains the audit record. Not nullable.<br /><br />**Applies to**: SQL Server only|
114
-
|`user_defined_event_id`|**smallint**| User defined event ID passed as an argument to `sp_audit_write`. `NULL` for system events (default) and non-zero for user-defined event. For more information, see [sp_audit_write (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-audit-write-transact-sql.md).<br /><br />**Applies to**: [!INCLUDE [ssSQL11](../../includes/sssql11-md.md)] and later, Azure SQL Database, and SQL Managed Instance|
115
-
|`user_defined_information`|**nvarchar(4000)**| Used to record any extra information the user wants to record in audit log by using the `sp_audit_write` stored procedure.<br /><br />**Applies to**: [!INCLUDE [ssSQL11](../../includes/sssql11-md.md)] and later versions, Azure SQL Database, and SQL Managed Instance |
121
+
|`audit_file_offset`|**bigint**| The buffer offset in the file that contains the audit record. Not nullable. |
122
+
|`user_defined_event_id`|**smallint**| User defined event ID passed as an argument to `sp_audit_write`. `NULL` for system events (default) and non-zero for user-defined event. For more information, see [sp_audit_write (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-audit-write-transact-sql.md). |
123
+
|`user_defined_information`|**nvarchar(4000)**| Used to record any extra information the user wants to record in audit log by using the `sp_audit_write` stored procedure.|
116
124
|`audit_schema_version`|**int**| Always `1`. |
117
-
|`sequence_group_id`|**varbinary**| Unique identifier.<br /><br />**Applies to**: [!INCLUDE [sssql16-md](../../includes/sssql16-md.md)] and later versions |
118
-
|`transaction_id`|**bigint**| Unique identifier to identify multiple audit events in one transaction.<br /><br />**Applies to**: [!INCLUDE [sssql16-md](../../includes/sssql16-md.md)] and later versions|
119
-
|`client_ip`|**nvarchar(128)**| Source IP of the client application.<br /><br />**Applies to**: [!INCLUDE [sssql17-md](../../includes/sssql17-md.md)] and later versions, and Azure SQL Database|
120
-
|`application_name`|**nvarchar(128)**| Name of client application that executed the statement that caused the audit event.<br /><br />**Applies to**: [!INCLUDE [sssql17-md](../../includes/sssql17-md.md)] and later versions, and Azure SQL Database |
121
-
|`duration_milliseconds`|**bigint**| Query execution duration in milliseconds.<br /><br />**Applies to**: Azure SQL Database and SQL Managed Instance|
122
-
|`response_rows`|**bigint**| Number of rows returned in the result set.<br /><br />**Applies to**: Azure SQL Database and SQL Managed Instance|
123
-
|`affected_rows`|**bigint**| Number of rows affected by the executed statement.<br /><br />**Applies to**: Azure SQL Database only |
124
-
|`connection_id`|**uniqueidentifier**| ID of the connection in the server.<br /><br />**Applies to**: Azure SQL Database and SQL Managed Instance|
125
-
|`data_sensitivity_information`|**nvarchar(4000)**| Information types and sensitivity labels returned by the audited query, based on the classified columns in the database. Learn more about [Azure SQL Database data discover and classification](/azure/sql-database/sql-database-data-discovery-and-classification).<br /><br />**Applies to**: Azure SQL Database only |
|`transaction_id`|**bigint**| Unique identifier to identify multiple audit events in one transaction. |
127
+
|`client_ip`|**nvarchar(128)**| Source IP of the client application. |
128
+
|`application_name`|**nvarchar(128)**| Name of client application that executed the statement that caused the audit event.|
129
+
|`duration_milliseconds`|**bigint**| Query execution duration in milliseconds. |
130
+
|`response_rows`|**bigint**| Number of rows returned in the result set.< |
131
+
|`affected_rows`|**bigint**| Number of rows affected by the executed statement.|
132
+
|`connection_id`|**uniqueidentifier**| ID of the connection in the server. |
133
+
|`data_sensitivity_information`|**nvarchar(4000)**| Information types and sensitivity labels returned by the audited query, based on the classified columns in the database. Learn more about [Azure SQL Database data discover and classification](/azure/sql-database/sql-database-data-discovery-and-classification).|
126
134
|`host_name`|**nvarchar(128)**| Host Name of the client machine. |
127
135
|`session_context`|**nvarchar(4000)**| The key-value pairs that are a part of the current session context. |
128
136
|`client_tls_version`|**bigint**| Minimum TLS version supported by the client. |
129
137
|`client_tls_version_name`|**nvarchar(128)**| Minimum TLS version supported by the client. |
130
138
|`database_transaction_id`|**bigint**| Transaction ID of the current transaction in the current session. |
131
-
|`ledger_start_sequence_number`|**bigint**| The sequence number of an operation within a transaction that created a row version.<br /><br />**Applies to:** Azure SQL Database only |
132
-
|`external_policy_permissions_checked`|**nvarchar(4000)**| Information related to the external authorization permission check, when an audit event is generated, and Purview external authorization policies are evaluated.<br /><br />**Applies to:** Azure SQL Database only |
133
-
|`obo_middle_tier_app_id`|**varchar(120)**| The application ID of the middle tier application that connects to Azure SQL Database using on-behalf-of (OBO) access. Nullable. Returns `NULL` if the request isn't made using OBO access.<br /><br />**Applies to**: Azure SQL Database only |
134
-
|`is_local_secondary_replica`|**bit**|`True` if the audit record originates from a read-only local secondary replica, `False` otherwise.<br /><br />**Applies to:** Azure SQL Database only |
139
+
|`ledger_start_sequence_number`|**bigint**| The sequence number of an operation within a transaction that created a row version.|
140
+
|`external_policy_permissions_checked`|**nvarchar(4000)**| Information related to the external authorization permission check, when an audit event is generated, and Purview external authorization policies are evaluated.|
141
+
|`obo_middle_tier_app_id`|**varchar(120)**| The application ID of the middle tier application that connects using on-behalf-of (OBO) access. Nullable. Returns `NULL` if the request isn't made using OBO access.|
142
+
|`is_local_secondary_replica`|**bit**|`True` if the audit record originates from a read-only local secondary replica, `False` otherwise.|
135
143
136
144
## Improvements over sys.fn_get_audit_file
137
145
@@ -151,16 +159,18 @@ The performance improvements are primarily dependent on the rollover time of the
151
159
152
160
-**Scalability**: Helps maintain performance even as the number of databases increases, though the net improvement might be less pronounced in environments with a high number of databases.
153
161
154
-
For information on setting up Azure SQL Database auditing, see [Get Started with SQL Database auditing](/azure/sql-database/sql-database-auditing).
155
-
156
162
## Remarks
157
163
158
-
- If the *file_pattern* argument passed to `fn_get_audit_file_v2` references a path or file that doesn't exist, or if the file isn't an audit file, the `MSG_INVALID_AUDIT_FILE` error message is returned.
164
+
If the *file_pattern* argument passed to `fn_get_audit_file_v2` references a path or file that doesn't exist, or if the file isn't an audit file, the `MSG_INVALID_AUDIT_FILE` error message is returned.
165
+
166
+
The `fn_get_audit_file_v2` function can't be used when the audit is created with the `APPLICATION_LOG`, `SECURITY_LOG`, or `EXTERNAL_MONITOR` options.
159
167
160
-
-`fn_get_audit_file_v2` can't be used when the audit is created with the `APPLICATION_LOG`, `SECURITY_LOG`, or `EXTERNAL_MONITOR` options.
168
+
Currently in Fabric Data Warehouse, you cannot access individual files, only the audit folder. The following arguments are not supported for SQL Audit on a warehouse item: `file_pattern`, `initial_file_name`, `audit_record_offset`.
161
169
162
170
## Permissions
163
171
172
+
### Permissions required in Azure SQL Database
173
+
164
174
Requires the `CONTROL DATABASE` permission.
165
175
166
176
- Server admins can access audit logs of all databases on the server.
@@ -169,14 +179,38 @@ Requires the `CONTROL DATABASE` permission.
169
179
170
180
- Blobs that don't meet the above criteria are skipped (a list of skipped blobs is displayed in the query output message). The function returns logs only from blobs for which access is allowed.
171
181
182
+
### Permissions required in Fabric Data Warehouse
183
+
184
+
Users must have the Fabric item `Audit` permission. For more information, see [Permissions](/fabric/data-warehouse/sql-audit-logs#permissions).
185
+
172
186
## Examples
173
187
188
+
### A. View SQL audit logs for Azure SQL Database
189
+
174
190
This example retrieves audit logs from a specific Azure Blob Storage location, filtering records between `2023-11-17T08:40:40Z` and `2023-11-17T09:10:40Z`.
### B. View SQL audit logs for Fabric Data Warehouse
203
+
204
+
This example retrieves audit logs from OneLake folder aligned with the current workspace and warehouse, filtering records between `2023-11-17T08:40:40Z` and `2023-11-17T09:10:40Z`.
205
+
206
+
In the Fabric portal, retrieve your `workspaceID` and `warehouseID`:
207
+
-`<workspaceID>`: Visit your workspace in the Fabric portal. Find the workspace GUID in the URL after the `/groups/` section, or by running `SELECT @@SERVERNAME` in an existing warehouse. If your `/groups/` URL is followed by `/me/`, you're using the default workspace, and currently SQL Audit for Fabric Data Warehouse is not supported in the default workspace.
208
+
-`<warehouseID>`: Visit your warehouse in the Fabric portal. Find the warehouse ID in the URL after the `/warehouses/` section.
0 commit comments