Skip to content

Commit bda772a

Browse files
authored
Merge pull request #34698 from rwestMSFT/rw-0714-qs-49912
Improve context for sys.query_store_query (49912)
2 parents 7db6e5e + ae6aa03 commit bda772a

File tree

2 files changed

+71
-65
lines changed

2 files changed

+71
-65
lines changed

docs/relational-databases/system-catalog-views/sys-query-store-query-transact-sql.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "sys.query_store_query (Transact-SQL)"
33
description: Contains information about the query and its associated overall aggregated runtime execution statistics.
44
author: rwestMSFT
55
ms.author: randolphwest
6-
ms.date: 12/16/2023
6+
ms.date: 07/14/2025
77
ms.service: sql
88
ms.subservice: system-objects
99
ms.topic: "reference"
@@ -28,9 +28,9 @@ Contains information about the query and its associated overall aggregated runti
2828
| Column name | Data type | Description |
2929
| --- | --- | --- |
3030
| `query_id` | **bigint** | Primary key. |
31-
| `query_text_id` | **bigint** | Foreign key. Joins to [sys.query_store_query_text (Transact-SQL)](sys-query-store-query-text-transact-sql.md) |
32-
| `context_settings_id` <sup>1</sup> | **bigint** | Foreign key. Joins to [sys.query_context_settings (Transact-SQL)](sys-query-context-settings-transact-sql.md). |
33-
| `object_id` <sup>2</sup> | **bigint** | ID of the database object that the query is part of (stored procedure, trigger, CLR UDF/UDAgg, etc.). `0` if the query isn't executed as part of a database object (ad hoc query). |
31+
| `query_text_id` | **bigint** | Foreign key. Joins to [sys.query_store_query_text](sys-query-store-query-text-transact-sql.md) |
32+
| `context_settings_id` <sup>1</sup> | **bigint** | Foreign key. Joins to [sys.query_context_settings](sys-query-context-settings-transact-sql.md). |
33+
| `object_id` <sup>2</sup> | **bigint** | ID of the database object that the query is part of (stored procedure, trigger, CLR UDF/UDAgg, etc.). `0` if the query isn't executed as part of a database object (ad hoc query). See the [Remarks](#remarks) section in this article. |
3434
| `batch_sql_handle` <sup>3</sup> | **varbinary(64)** | ID of the statement batch the query is part of. Populated only if query references temporary tables or table variables. |
3535
| `query_hash` | **binary(8)** | Zobrist hash over the shape of the individual query, based on the bound (input) logical query tree. Query hints aren't included as part of the hash. |
3636
| `is_internal_query` <sup>2</sup> | **bit** | The query was generated internally. |
@@ -39,7 +39,7 @@ Contains information about the query and its associated overall aggregated runti
3939
| `initial_compile_start_time` | **datetimeoffset** | Initial compile start time. |
4040
| `last_compile_start_time` | **datetimeoffset** | Most recent compile start time. |
4141
| `last_execution_time` | **datetimeoffset** | Last execution time refers to the last end time of the query/plan. |
42-
| `last_compile_batch_sql_handle` | **varbinary(64)** | Handle of the last SQL batch in which query was used last time. It can be provided as input to [sys.dm_exec_sql_text (Transact-SQL)](../system-dynamic-management-views/sys-dm-exec-sql-text-transact-sql.md) to get the full text of the batch. |
42+
| `last_compile_batch_sql_handle` | **varbinary(64)** | Handle of the last SQL batch in which query was used last time. It can be provided as input to [sys.dm_exec_sql_text](../system-dynamic-management-views/sys-dm-exec-sql-text-transact-sql.md) to get the full text of the batch. |
4343
| `last_compile_batch_offset_start` <sup>2</sup> | **bigint** | Information that can be provided to `sys.dm_exec_sql_text` along with `last_compile_batch_sql_handle`. |
4444
| `last_compile_batch_offset_end` <sup>2</sup> | **bigint** | Information that can be provided to `sys.dm_exec_sql_text` along with `last_compile_batch_sql_handle`. |
4545
| `count_compiles` <sup>1</sup> | **bigint** | Compilation statistics. |
@@ -66,9 +66,15 @@ Contains information about the query and its associated overall aggregated runti
6666

6767
<sup>4</sup> Azure Synapse Analytics always returns `None`.
6868

69+
## Remarks
70+
71+
The `object_id` column is populated only when the statement is compiled from a Transact‑SQL module. A module is any schema‑scoped object that has a row in [sys.sql_modules](sys-sql-modules-transact-sql.md).
72+
73+
Because the query optimizer expands non-indexed views before it produces a plan, only the underlying tables remain, though indexed views do appear as tables.
74+
6975
## Permissions
7076

71-
Requires the **VIEW DATABASE STATE** permission.
77+
Requires the `VIEW DATABASE STATE` permission.
7278

7379
## Related content
7480

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
title: "sys.sql_modules (Transact-SQL)"
3-
description: sys.sql_modules (Transact-SQL)
3+
description: sys.sql_modules returns a row for each object that is a SQL language-defined module in SQL Server.
44
author: rwestMSFT
55
ms.author: randolphwest
6-
ms.date: 08/20/2021
6+
ms.date: 07/14/2025
77
ms.service: sql
88
ms.subservice: system-objects
99
ms.topic: "reference"
@@ -16,65 +16,65 @@ helpviewer_keywords:
1616
- "sys.sql_modules catalog view"
1717
dev_langs:
1818
- "TSQL"
19-
monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current||=fabric"
19+
monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric"
2020
---
2121
# sys.sql_modules (Transact-SQL)
22+
2223
[!INCLUDE [sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw.md)]
2324

24-
Returns a row for each object that is a SQL language-defined module in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)], including natively compiled scalar user-defined function. Objects of type P, RF, V, TR, FN, IF, TF, and R have an associated SQL module. Stand-alone defaults, objects of type D, also have a SQL module definition in this view. For a description of these types, see the `type` column in the [sys.objects](../../relational-databases/system-catalog-views/sys-objects-transact-sql.md) catalog view.
25-
26-
For more information, see [Scalar User-Defined Functions for In-Memory OLTP](../../relational-databases/in-memory-oltp/scalar-user-defined-functions-for-in-memory-oltp.md).
27-
28-
|Column name|Data type|Description|
29-
|-----------------|---------------|-----------------|
30-
|**object_id**|**int**|ID of the object of the containing object. Is unique within a database.|
31-
|**definition**|**nvarchar(max)**|SQL text that defines this module. This value can also be obtained using the [OBJECT_DEFINITION](../../t-sql/functions/object-definition-transact-sql.md) built-in function.<br /><br /> NULL = Encrypted.|
32-
|**uses_ansi_nulls**|**bit**|Module was created with SET ANSI_NULLS ON.<br /><br /> Will always be = 0 for rules and defaults.|
33-
|**uses_quoted_identifier**|**bit**|Module was created with SET QUOTED_IDENTIFIER ON.|
34-
|**is_schema_bound**|**bit**|Module was created with SCHEMABINDING option.<br /><br /> Always contains a value of 1 for natively compiled stored procedures.|
35-
|**uses_database_collation**|**bit**|1 = Schema-bound module definition depends on the default-collation of the database for correct evaluation; otherwise, 0. Such a dependency prevents changing the database's default collation.|
36-
|**is_recompiled**|**bit**|Procedure was created WITH RECOMPILE option.|
37-
|**null_on_null_input**|**bit**|Module was declared to produce a `NULL` output on any `NULL` input.|
38-
|**execute_as_principal_id**|**Int**|ID of the EXECUTE AS database principal.<br /><br /> `NULL` by default or if EXECUTE AS CALLER.<br /><br /> ID of the specified principal if EXECUTE AS SELF or EXECUTE AS \<principal>.<br /><br /> -2 = EXECUTE AS OWNER.|
39-
|**uses_native_compilation**|**bit**|**Applies to**: [!INCLUDE[ssSQL14](../../includes/sssql14-md.md)] through [!INCLUDE[ssSQL14](../../includes/sssql14-md.md)].<br /><br /> 0 = not natively compiled<br /><br /> 1 = is natively compiled<br /><br /> The default value is 0.|
40-
|**is_inlineable**|**bit**|**Applies to**: [!INCLUDE[sssql19-md](../../includes/sssql19-md.md)] and later.<br/><br />Indicates whether the module is inlineable or not. Inlineability is based on the conditions specified [here](../user-defined-functions/scalar-udf-inlining.md#requirements).<br /><br /> 0 = not inlineable<br /><br /> 1 = is inlineable. <br /><br /> For scalar user-defined functions (UDFs), the value will be 1 if the UDF is inlineable, and 0 otherwise. It always contains a value of 1 for inline table-valued functions (TVFs), and 0 for all other module types.<br />|
41-
|**inline_type**|**bit**|**Applies to**: [!INCLUDE[sssql19-md](../../includes/sssql19-md.md)] and later.<br /><br />Indicates whether inlining is turned on for the module currently. <br /><br />0 = inlining is turned off<br /><br /> 1 = inlining is turned on.<br /><br /> For scalar user-defined functions (UDFs), the value will be 1 if inlining is turned on (explicitly or implicitly). The value will always be 1 for inline table-valued functions (TVFs), and 0 for other module types.<br />|
25+
Returns a row for each object that is a SQL language-defined module in [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)], including natively compiled scalar user-defined function. Objects of type `P`, `RF`, `V`, `TR`, `FN`, `IF`, `TF`, and `R` have an associated SQL module. Stand-alone defaults, objects of type `D`, also have a SQL module definition in this view. For a description of these types, see the `type` column in the [sys.objects](sys-objects-transact-sql.md) catalog view.
26+
27+
For more information, see [Scalar User-Defined Functions for In-Memory OLTP](../in-memory-oltp/scalar-user-defined-functions-for-in-memory-oltp.md).
28+
29+
| Column name | Data type | Description |
30+
| --- | --- | --- |
31+
| `object_id` | **int** | ID of the object of the containing object. Is unique within a database. |
32+
| `definition` | **nvarchar(max)** | SQL text that defines this module. This value can also be obtained using the [OBJECT_DEFINITION](../../t-sql/functions/object-definition-transact-sql.md) built-in function.<br /><br />`NULL` = Encrypted. |
33+
| `uses_ansi_nulls` | **bit** | Module was created with `SET ANSI_NULLS ON`.<br /><br />Always `0` for rules and defaults. |
34+
| `uses_quoted_identifier` | **bit** | Module was created with `SET QUOTED_IDENTIFIER ON`. |
35+
| `is_schema_bound` | **bit** | Module was created with `SCHEMABINDING` option.<br /><br />Always contains a value of `1` for natively compiled stored procedures. |
36+
| `uses_database_collation` | **bit** | `1` = Schema-bound module definition depends on the default-collation of the database for correct evaluation; otherwise, `0`. Such a dependency prevents changing the database's default collation. |
37+
| `is_recompiled` | **bit** | Procedure was created `WITH RECOMPILE` option. |
38+
| `null_on_null_input` | **bit** | Module was declared to produce a `NULL` output on any `NULL` input. |
39+
| `execute_as_principal_id` | **Int** | ID of the `EXECUTE AS` database principal.<br /><br />`NULL` by default or if `EXECUTE AS CALLER`.<br />ID of the specified principal if `EXECUTE AS SELF` or `EXECUTE AS <principal>`.<br />`-2` = `EXECUTE AS OWNER`. |
40+
| `uses_native_compilation` | **bit** | `0` = not natively compiled<br />`1` = is natively compiled<br /><br />The default value is `0`.<br /><br /> **Applies to**: [!INCLUDE [ssSQL14](../../includes/sssql14-md.md)] and later versions. |
41+
| `is_inlineable` | **bit** | Indicates whether the module can be inlined or not. Inlineability is based on the conditions specified in the [requirements](../user-defined-functions/scalar-udf-inlining.md#requirements).<br /><br />`0` = can't be inlined<br />`1` = can be inlined.<br /><br />For scalar user-defined functions (UDFs), the value is `1` if the UDF can be inlined, and `0` otherwise. It always contains a value of `1` for inline table-valued functions (TVFs), and `0` for all other module types.<br /><br />**Applies to**: [!INCLUDE [sssql19-md](../../includes/sssql19-md.md)] and later versions. |
42+
| `inline_type` | **bit** | Indicates whether inlining is turned on for the module currently.<br />0 = inlining is turned off<br />1 = inlining is turned on.<br />For scalar user-defined functions (UDFs), the value is `1` if inlining is turned on (explicitly or implicitly). The value is always `1` for inline table-valued functions (TVFs), and `0` for other module types.<br /><br />**Applies to**: [!INCLUDE [sssql19-md](../../includes/sssql19-md.md)] and later versions. |
43+
44+
## Remarks
45+
46+
The SQL expression for a `DEFAULT` constraint, object of type `D`, is found in the [sys.default_constraints](sys-default-constraints-transact-sql.md) catalog view. The SQL expression for a `CHECK` constraint, object of type `C`, is found in the [sys.check_constraints](sys-check-constraints-transact-sql.md) catalog view.
47+
48+
This information is also described in [sys.dm_db_uncontained_entities](../system-dynamic-management-views/sys-dm-db-uncontained-entities-transact-sql.md).
49+
50+
Renaming a stored procedure, function, view, or trigger doesn't change the name of the corresponding object in the definition column of the `sys.sql_modules` catalog view or the definition returned by the [OBJECT_DEFINITION](../../t-sql/functions/object-definition-transact-sql.md) built-in function. For this reason, we recommend that you don't use `sp_rename` to rename these object types. Instead, drop and recreate the object with its new name. Learn more in [sp_rename](../system-stored-procedures/sp-rename-transact-sql.md).
51+
52+
## Permissions
53+
54+
[!INCLUDE [ssCatViewPerm](../../includes/sscatviewperm-md.md)] For more information, see [Metadata Visibility Configuration](../security/metadata-visibility-configuration.md).
55+
56+
## Examples
57+
58+
The following example returns the object_id, schema name, object name, object type, and definition of each module in the current database.
59+
60+
```sql
61+
SELECT sm.object_id,
62+
ss.[name] AS [schema],
63+
o.[name] AS object_name,
64+
o.[type],
65+
o.[type_desc],
66+
sm.[definition]
67+
FROM sys.sql_modules AS sm
68+
INNER JOIN sys.objects AS o
69+
ON sm.object_id = o.object_id
70+
INNER JOIN sys.schemas AS ss
71+
ON o.schema_id = ss.schema_id
72+
ORDER BY o.[type], ss.[name], o.[name];
73+
```
74+
75+
## Related content
4276

43-
44-
## Remarks
45-
The SQL expression for a DEFAULT constraint, object of type D, is found in the [sys.default_constraints](../../relational-databases/system-catalog-views/sys-default-constraints-transact-sql.md) catalog view. The SQL expression for a CHECK constraint, object of type C, is found in the [sys.check_constraints](../../relational-databases/system-catalog-views/sys-check-constraints-transact-sql.md) catalog view.
46-
47-
This information is also described in [sys.dm_db_uncontained_entities &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/sys-dm-db-uncontained-entities-transact-sql.md).
48-
49-
Renaming a stored procedure, function, view, or trigger won't change the name of the corresponding object in the definition column of the `sys.sql_modules` catalog view or the definition returned by the [OBJECT_DEFINITION](../../t-sql/functions/object-definition-transact-sql.md) built-in function. For this reason, we recommend that `sp_rename` not be used to rename these object types. Instead, drop and re-create the object with its new name. Learn more in [sp_rename (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-rename-transact-sql.md).
50-
51-
## Permissions
52-
[!INCLUDE[ssCatViewPerm](../../includes/sscatviewperm-md.md)] For more information, see [Metadata Visibility Configuration](../../relational-databases/security/metadata-visibility-configuration.md).
53-
54-
## Examples
55-
The following example returns the object_id, schema name, object name, object type, and definition of each module in the current database.
56-
57-
```sql
58-
SELECT
59-
sm.object_id
60-
, ss.[name] as [schema]
61-
, o.[name] as object_name
62-
, o.[type]
63-
, o.[type_desc]
64-
, sm.[definition]
65-
FROM sys.sql_modules AS sm
66-
JOIN sys.objects AS o
67-
ON sm.object_id = o.object_id
68-
JOIN sys.schemas AS ss
69-
ON o.schema_id = ss.schema_id
70-
ORDER BY
71-
o.[type]
72-
, ss.[name]
73-
, o.[name];
74-
```
75-
76-
## See Also
77-
[Catalog Views &#40;Transact-SQL&#41;](../../relational-databases/system-catalog-views/catalog-views-transact-sql.md)
78-
[Object Catalog Views &#40;Transact-SQL&#41;](../../relational-databases/system-catalog-views/object-catalog-views-transact-sql.md)
79-
[Querying the SQL Server System Catalog FAQ](../../relational-databases/system-catalog-views/querying-the-sql-server-system-catalog-faq.yml)
80-
[In-Memory OLTP &#40;In-Memory Optimization&#41;](../in-memory-oltp/overview-and-usage-scenarios.md)
77+
- [System catalog views (Transact-SQL)](catalog-views-transact-sql.md)
78+
- [Object catalog views (Transact-SQL)](object-catalog-views-transact-sql.md)
79+
- [Querying the SQL Server System Catalog FAQ](querying-the-sql-server-system-catalog-faq.yml)
80+
- [In-Memory OLTP overview and usage scenarios](../in-memory-oltp/overview-and-usage-scenarios.md)

0 commit comments

Comments
 (0)