Skip to content

Commit cb0ce05

Browse files
authored
Merge pull request #33563 from WilliamDAssafMSFT/20250320-sys.fn_get_audit_file_v2
20250320 add Fabric DW
2 parents 5d62c1a + 31e22d0 commit cb0ce05

File tree

2 files changed

+75
-31
lines changed

2 files changed

+75
-31
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
author: WilliamDAssafMSFT
3+
ms.author: wiassaf
4+
ms.date: 03/20/2025
5+
ms.service: sql
6+
ms.topic: include
7+
---
8+
[!INCLUDE [Applies to](../../includes/applies-md.md)] [!INCLUDE [Azure SQL Database](../../includes/applies-to-version/_asdb.md)] [!INCLUDE [Fabric DW](_fabric-dw.md)]

docs/relational-databases/system-functions/sys-fn-get-audit-file-v2-transact-sql.md

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
title: "sys.fn_get_audit_file_v2 (Transact-SQL)"
33
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."
4-
author: sravanisaluru
5-
ms.author: srsaluru
6-
ms.reviewer: randolphwest
7-
ms.date: 06/12/2024
4+
author: WilliamDAssafMSFT
5+
ms.author: wiassaf
6+
ms.reviewer: randolphwest, srsaluru, fresantos
7+
ms.date: 03/21/2025
88
ms.service: sql
99
ms.subservice: system-objects
1010
ms.topic: "reference"
@@ -18,19 +18,21 @@ helpviewer_keywords:
1818
- "fn_get_audit_file_v2 function"
1919
dev_langs:
2020
- "TSQL"
21-
monikerRange: "=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =azure-sqldw-latest"
21+
monikerRange: "=azuresqldb-current || =fabric"
2222
---
2323
# sys.fn_get_audit_file_v2 (Transact-SQL)
2424

25-
[!INCLUDE [asdb](../../includes/applies-to-version/asdb.md)]
25+
[!INCLUDE [asdb-fabricdw](../../includes/applies-to-version/asdb-fabricdw.md)]
2626

27-
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.
3128

3229
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).
3330

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).
34+
35+
3436
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
3537

3638
## Syntax
@@ -47,6 +49,8 @@ fn_get_audit_file_v2 ( file_pattern
4749

4850
#### *file_pattern*
4951

52+
**Applies to:** [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)] only
53+
5054
Specifies the directory or path and file name for the audit file set to be read. *file_pattern* is **nvarchar(260)**.
5155

5256
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
5963

6064
#### *initial_file_name*
6165

66+
**Applies to:** [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)] only
67+
6268
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)**.
6369

6470
The *initial_file_name* argument must contain valid entries or must contain either the `default` or `NULL` value.
6571

6672
#### *audit_record_offset*
6773

74+
**Applies to:** [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)] only
75+
6876
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.
6977

7078
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.
110118
| `statement` | **nvarchar(4000)** | Transact-SQL statement if it exists. Nullable. Returns `NULL` if not applicable. |
111119
| `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. |
112120
| `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.|
116124
| `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 |
125+
| `sequence_group_id` | **varbinary** | Unique identifier.<|
126+
| `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).|
126134
| `host_name` | **nvarchar(128)** | Host Name of the client machine. |
127135
| `session_context` | **nvarchar(4000)** | The key-value pairs that are a part of the current session context. |
128136
| `client_tls_version` | **bigint** | Minimum TLS version supported by the client. |
129137
| `client_tls_version_name` | **nvarchar(128)** | Minimum TLS version supported by the client. |
130138
| `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.|
135143

136144
## Improvements over sys.fn_get_audit_file
137145

@@ -151,16 +159,18 @@ The performance improvements are primarily dependent on the rollover time of the
151159

152160
- **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.
153161

154-
For information on setting up Azure SQL Database auditing, see [Get Started with SQL Database auditing](/azure/sql-database/sql-database-auditing).
155-
156162
## Remarks
157163

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.
159167

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`.
161169

162170
## Permissions
163171

172+
### Permissions required in Azure SQL Database
173+
164174
Requires the `CONTROL DATABASE` permission.
165175

166176
- Server admins can access audit logs of all databases on the server.
@@ -169,14 +179,38 @@ Requires the `CONTROL DATABASE` permission.
169179

170180
- 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.
171181

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+
172186
## Examples
173187

188+
### A. View SQL audit logs for Azure SQL Database
189+
174190
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`.
175191

176192
```sql
177193
SELECT *
178194
FROM sys. fn_get_audit_file_v2(
179-
'https://yourstorageaccount.blob.core.windows.net/sqldbauditlogs/server_name/database_name/SqlDbAuditing_ServerAudit/',
195+
'https://<storage_account>.blob.core.windows.net/sqldbauditlogs/server_name/database_name/SqlDbAuditing_ServerAudit/',
196+
DEFAULT,
197+
DEFAULT,
198+
'2023-11-17T08:40:40Z',
199+
'2023-11-17T09:10:40Z')
200+
```
201+
202+
### 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.
209+
210+
```sql
211+
SELECT *
212+
FROM sys. fn_get_audit_file_v2(
213+
'https://onelake.blob.fabric.microsoft.com/{workspaceId}/{warehouseId}/Audit/sqldbauditlogs/',
180214
DEFAULT,
181215
DEFAULT,
182216
'2023-11-17T08:40:40Z',
@@ -213,3 +247,5 @@ Transact-SQL:
213247
- [sys.dm_audit_class_type_map (Transact-SQL)](../system-dynamic-management-views/sys-dm-audit-class-type-map-transact-sql.md)
214248
- [sys.server_audits (Transact-SQL)](../system-catalog-views/sys-server-audits-transact-sql.md)
215249
- [sys.server_file_audits (Transact-SQL)](../system-catalog-views/sys-server-file-audits-transact-sql.md)
250+
- [Get Started with SQL Database auditing](/azure/sql-database/sql-database-auditing?view=azuresql-db&preserve-view=true)
251+
- [SQL Audit Logs in Fabric Data Warehouse](/fabric/data-warehouse/sql-audit-logs)

0 commit comments

Comments
 (0)