Skip to content

Commit 3f00d99

Browse files
authored
Merge pull request #32015 from rwestMSFT/rw-1008-fix-9900
Update sys-fn-stmt-sql-handle-from-sql-stmt (PR 9900)
2 parents f2dceb0 + a438493 commit 3f00d99

1 file changed

Lines changed: 52 additions & 46 deletions

File tree

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
---
22
title: "sys.fn_stmt_sql_handle_from_sql_stmt (Transact-SQL)"
3-
description: "sys.fn_stmt_sql_handle_from_sql_stmt (Transact-SQL)"
3+
description: Gets the stmt_sql_handle for a Transact-SQL statement under a given parameterization type (simple or forced).
44
author: rwestMSFT
55
ms.author: randolphwest
6-
ms.date: 09/09/2023
6+
ms.date: 10/08/2024
77
ms.service: sql
88
ms.subservice: system-objects
99
ms.topic: "reference"
1010
dev_langs:
1111
- "TSQL"
12-
monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
12+
monikerRange: "=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current"
1313
---
1414

1515
# sys.fn_stmt_sql_handle_from_sql_stmt (Transact-SQL)
1616

1717
[!INCLUDE [sqlserver2016-asdb-asdbmi](../../includes/applies-to-version/sqlserver2016-asdb-asdbmi.md)]
1818

19-
Gets the **stmt_sql_handle** for a [!INCLUDE [tsql](../../includes/tsql-md.md)] statement under given parameterization type (simple or forced). This allows you to refer to queries stored in the Query Store by using their **stmt_sql_handle** when you know their text.
19+
Gets the `stmt_sql_handle` for a [!INCLUDE [tsql](../../includes/tsql-md.md)] statement under the given parameterization type (simple or forced). You can refer to queries stored in the Query Store by using their `stmt_sql_handle` when you know their text.
2020

2121
:::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)
2222

@@ -25,78 +25,84 @@ Gets the **stmt_sql_handle** for a [!INCLUDE [tsql](../../includes/tsql-md.md)]
2525
```syntaxsql
2626
sys.fn_stmt_sql_handle_from_sql_stmt
2727
(
28-
'query_sql_text' ,
29-
[ query_param_type
30-
) [;]
28+
N'query_sql_text'
29+
, [ query_param_type ]
30+
)
31+
[ ; ]
3132
```
3233

3334
## Arguments
3435

35-
*query_sql_text*
36-
Is the text of the query in the query store that you want the handle of. *query_sql_text* is a **nvarchar(max)**, with no default.
36+
#### *query_sql_text*
3737

38-
*query_param_type*
39-
Is the parameter type of the query. *query_param_type* is a **tinyint**. Possible values are:
38+
The text of the query in the query store that you want the handle of. *query_sql_text* is **nvarchar(max)** with no default.
4039

41-
- NULL - defaults to 0
40+
#### *query_param_type*
4241

43-
- 0 - None
42+
The parameter type of the query. *query_param_type* is **tinyint**, with a default of `NULL`. Possible values are:
4443

45-
- 1 - User
44+
| Value | Description |
45+
| --- | --- |
46+
| `NULL` (default) | Defaults to `0` |
47+
| 0 | None |
48+
| 1 | User |
49+
| 2 | Simple |
50+
| 3 | Forced |
4651

47-
- 2 - Simple
48-
49-
- 3 - Forced
50-
51-
## Columns Returned
52+
## Columns returned
5253

5354
The following table lists the columns that `sys.fn_stmt_sql_handle_from_sql_stmt` returns.
5455

5556
| Column name | Type | Description |
5657
| --- | --- | --- |
57-
| **statement_sql_handle** | **varbinary(64)** | The SQL handle. |
58-
| **query_sql_text** | **nvarchar(max)** | The text of the [!INCLUDE [tsql](../../includes/tsql-md.md)] statement. |
59-
| **query_parameterization_type** | **tinyint** | The query parameterization type. |
58+
| `statement_sql_handle` | **varbinary(64)** | The SQL handle. |
59+
| `query_sql_text` | **nvarchar(max)** | The text of the [!INCLUDE [tsql](../../includes/tsql-md.md)] statement. |
60+
| `query_parameterization_type` | **tinyint** | The query parameterization type. |
6061

6162
## Return code values
6263

63-
0 (success) or 1 (failure)
64-
65-
## Remarks
64+
`0` (success) or `1` (failure).
6665

6766
## Permissions
6867

69-
Requires the **EXECUTE** permission on the database, and **DELETE** permission on the query store catalog views.
68+
Requires `EXECUTE` permission on the database, and `DELETE` permission on the query store catalog views.
7069

7170
## Examples
7271

7372
The following example executes a statement, and then uses `sys.fn_stmt_sql_handle_from_sql_stmt` to return the SQL handle of that statement.
7473

75-
```
76-
SELECT * FROM sys.databases;
77-
SELECT * FROM sys.fn_stmt_sql_handle_from_sql_stmt('SELECT * FROM sys.databases', NULL);
74+
```sql
75+
SELECT *
76+
FROM sys.databases;
77+
78+
SELECT *
79+
FROM sys.fn_stmt_sql_handle_from_sql_stmt('SELECT * FROM sys.databases', NULL);
7880
```
7981

8082
Use the function to correlate Query Store data with other dynamic management views. The following example:
8183

82-
```
83-
SELECT qt.query_text_id, q.query_id, qt.query_sql_text, qt.statement_sql_handle,
84-
q.context_settings_id, qs.statement_context_id
84+
```sql
85+
SELECT qt.query_text_id,
86+
q.query_id,
87+
qt.query_sql_text,
88+
qt.statement_sql_handle,
89+
q.context_settings_id,
90+
qs.statement_context_id
8591
FROM sys.query_store_query_text AS qt
86-
JOIN sys.query_store_query AS q
87-
ON qt.query_text_id = q.query_id
88-
CROSS APPLY sys.fn_stmt_sql_handle_from_sql_stmt (qt.query_sql_text, null) AS fn_handle_from_stmt
89-
JOIN sys.dm_exec_query_stats AS qs
90-
ON fn_handle_from_stmt.statement_sql_handle = qs.statement_sql_handle;
92+
INNER JOIN sys.query_store_query AS q
93+
ON qt.query_text_id = q.query_text_id
94+
CROSS APPLY sys.fn_stmt_sql_handle_from_sql_stmt(qt.query_sql_text, NULL) AS fn_handle_from_stmt
95+
INNER JOIN sys.dm_exec_query_stats AS qs
96+
ON fn_handle_from_stmt.statement_sql_handle = qs.statement_sql_handle;
9197
```
9298

93-
## See also
99+
## Related content
94100

95-
- [sp_query_store_force_plan (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-query-store-force-plan-transact-sql.md)
96-
- [sp_query_store_remove_plan (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-query-store-remove-plan-transact-sql.md)
97-
- [sp_query_store_unforce_plan (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-query-store-unforce-plan-transact-sql.md)
98-
- [sp_query_store_reset_exec_stats (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-query-store-reset-exec-stats-transact-sql.md)
99-
- [sp_query_store_flush_db (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-query-store-flush-db-transact-sql.md)
100-
- [sp_query_store_remove_query (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-query-store-remove-query-transact-sql.md)
101-
- [Query Store Catalog Views (Transact-SQL)](../../relational-databases/system-catalog-views/query-store-catalog-views-transact-sql.md)
102-
- [Monitoring Performance By Using the Query Store](../../relational-databases/performance/monitoring-performance-by-using-the-query-store.md)
101+
- [sp_query_store_force_plan (Transact-SQL)](../system-stored-procedures/sp-query-store-force-plan-transact-sql.md)
102+
- [sp_query_store_remove_plan (Transact-SQL)](../system-stored-procedures/sp-query-store-remove-plan-transact-sql.md)
103+
- [sp_query_store_unforce_plan (Transact-SQL)](../system-stored-procedures/sp-query-store-unforce-plan-transact-sql.md)
104+
- [sp_query_store_reset_exec_stats (Transact-SQL)](../system-stored-procedures/sp-query-store-reset-exec-stats-transact-sql.md)
105+
- [sp_query_store_flush_db (Transact-SQL)](../system-stored-procedures/sp-query-store-flush-db-transact-sql.md)
106+
- [sp_query_store_remove_query (Transact-SQL)](../system-stored-procedures/sp-query-store-remove-query-transact-sql.md)
107+
- [Query Store catalog views (Transact-SQL)](../system-catalog-views/query-store-catalog-views-transact-sql.md)
108+
- [Monitor performance by using the Query Store](../performance/monitoring-performance-by-using-the-query-store.md)

0 commit comments

Comments
 (0)