How sys.dm_exec_describe_first_result_set Works in SQL Server

In SQL Server, the sys.dm_exec_describe_first_result_set dynamic management function returns the metadata of the first result set for a given T-SQL statement or statements.

This function uses the same algorithm as the sp_describe_first_result_set system stored procedure, and does pretty much the same thing.

It accepts three parameters, the first of which is the T-SQL statement/s you’re analysing.

Syntax

The syntax goes like this:

sys.dm_exec_describe_first_result_set(@tsql, @params, @include_browse_information)

Example

Here’s an example to demonstrate.

SELECT * 
FROM sys.dm_exec_describe_first_result_set(
    N'SELECT * FROM Artists', 
    null, 
    0
);

Result:

+-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
| is_hidden   | column_ordinal   | name       | is_nullable   | system_type_id   | system_type_name   | max_length   | precision   | scale   | collation_name               | user_type_id   | user_type_database   | user_type_schema   | user_type_name   | assembly_qualified_type_name   | xml_collection_id   | xml_collection_database   | xml_collection_schema   | xml_collection_name   | is_xml_document   | is_case_sensitive   | is_fixed_length_clr_type   | source_server   | source_database   | source_schema   | source_table   | source_column   | is_identity_column   | is_part_of_unique_key   | is_updateable   | is_computed_column   | is_sparse_column_set   | ordinal_in_order_by_list   | order_by_is_descending   | order_by_list_length   | error_number   | error_severity   | error_state   | error_message   | error_type   | error_type_desc   |
|-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------|
| 0           | 1                | ArtistId   | 0             | 56               | int                | 4            | 10          | 0       | NULL                         | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | NULL            | NULL              | NULL            | NULL           | NULL            | 1                    | NULL                    | 0               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
| 0           | 2                | ArtistName | 0             | 231              | nvarchar(255)      | 510          | 0           | 0       | SQL_Latin1_General_CP1_CI_AS | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | NULL            | NULL              | NULL            | NULL           | NULL            | 0                    | NULL                    | 1               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
| 0           | 3                | ActiveFrom | 1             | 40               | date               | 3            | 10          | 0       | NULL                         | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | NULL            | NULL              | NULL            | NULL           | NULL            | 0                    | NULL                    | 1               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
+-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

This function returns quite a few columns, so you’ll probably need to scroll sideways to see all of them. Feel free to check out Microsoft’s documentation for an explanation of each column returned.

Browse Mode

The third argument – @include_browse_information – specifies if additional key columns and source table information are returned.

The following values are accepted for this argument:

ValueResult
0No information is returned.
1Each query is analysed as if it includes a FOR BROWSE option on the query. This will return base table names as the source column information.
2Each query is analysed as if it would be used in preparing or executing a cursor. This will return view names as source column information.

Below are examples that illustrate how each of these values affect the result.

To make things more concise, I’ll modify my T-SQL statement to return only one column.

I should mention that the Microsoft documentation for sys.dm_exec_describe_first_result_set only refers to just two values: 0 and 1. However, the documentation for sp_describe_first_result_set specifies three values: 0, 1, and 2, as listed in the above table.

The documentation for sys.dm_exec_describe_first_result_set also states that it uses the same algorithm as sp_describe_first_result_set.

I also checked this function in both SQL Server 2017 and SQL Server 2019, and this parameter is actually a tinyint, which suggests it was designed to accept more than two values.

Either way, the following examples include all three values.

@include_browse_information = 0

In this example I set @include_browse_information to 0.

SELECT * 
FROM sys.dm_exec_describe_first_result_set(
    N'SELECT AlbumName FROM vAlbums', 
    null, 
    0
);

Result (using vertical output):

is_hidden                    | 0
column_ordinal               | 1
name                         | AlbumName
is_nullable                  | 0
system_type_id               | 231
system_type_name             | nvarchar(255)
max_length                   | 510
precision                    | 0
scale                        | 0
collation_name               | SQL_Latin1_General_CP1_CI_AS
user_type_id                 | NULL
user_type_database           | NULL
user_type_schema             | NULL
user_type_name               | NULL
assembly_qualified_type_name | NULL
xml_collection_id            | NULL
xml_collection_database      | NULL
xml_collection_schema        | NULL
xml_collection_name          | NULL
is_xml_document              | 0
is_case_sensitive            | 0
is_fixed_length_clr_type     | 0
source_server                | NULL
source_database              | NULL
source_schema                | NULL
source_table                 | NULL
source_column                | NULL
is_identity_column           | 0
is_part_of_unique_key        | NULL
is_updateable                | 1
is_computed_column           | 0
is_sparse_column_set         | 0
ordinal_in_order_by_list     | NULL
order_by_is_descending       | NULL
order_by_list_length         | NULL
error_number                 | NULL
error_severity               | NULL
error_state                  | NULL
error_message                | NULL
error_type                   | NULL
error_type_desc              | NULL

Notice that quite a few columns are NULL. In particular, notice that the source_database, source_schema, source_table, and source_column columns are NULL.

These columns won’t be NULL in the next two examples, yet they will produce two different results.

@include_browse_information = 1

In this example I set @include_browse_information to 1.

SELECT * 
FROM sys.dm_exec_describe_first_result_set(
    N'SELECT AlbumName FROM vAlbums', 
    null, 
    1
);

Setting @include_browse_information to 1 returns the result as if it includes a FOR BROWSE option on the query.

In our case, this now results in four rows being returned (representing four columns from the base tables).

Here are the four rows returned:

+-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
| is_hidden   | column_ordinal   | name      | is_nullable   | system_type_id   | system_type_name   | max_length   | precision   | scale   | collation_name               | user_type_id   | user_type_database   | user_type_schema   | user_type_name   | assembly_qualified_type_name   | xml_collection_id   | xml_collection_database   | xml_collection_schema   | xml_collection_name   | is_xml_document   | is_case_sensitive   | is_fixed_length_clr_type   | source_server   | source_database   | source_schema   | source_table   | source_column   | is_identity_column   | is_part_of_unique_key   | is_updateable   | is_computed_column   | is_sparse_column_set   | ordinal_in_order_by_list   | order_by_is_descending   | order_by_list_length   | error_number   | error_severity   | error_state   | error_message   | error_type   | error_type_desc   |
|-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------|
| 0           | 1                | AlbumName | 0             | 231              | nvarchar(255)      | 510          | 0           | 0       | SQL_Latin1_General_CP1_CI_AS | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | Homer           | Music             | dbo             | Albums         | AlbumName       | 0                    | 0                       | 1               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
| 1           | 2                | ArtistId  | 0             | 56               | int                | 4            | 10          | 0       | NULL                         | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | Homer           | Music             | dbo             | Artists        | ArtistId        | 0                    | 1                       | 1               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
| 1           | 3                | AlbumId   | 0             | 56               | int                | 4            | 10          | 0       | NULL                         | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | Homer           | Music             | dbo             | Albums         | AlbumId         | 0                    | 1                       | 1               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
| 1           | 4                | GenreId   | 0             | 56               | int                | 4            | 10          | 0       | NULL                         | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | Homer           | Music             | dbo             | Genres         | GenreId         | 0                    | 1                       | 1               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
+-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

So even though I only specified just one column in the T-SQL query that I passed to the procedure, it returned info for four columns. These are the four columns that are referenced by the vAlbums view.

If you scroll sideways far enough, you’ll see that the source_database, source_schema, source_table, and source_column columns are no longer NULL. Neither is the source_server column. They provide information about the base objects queried by the view.

To make it easier to see, let’s use vertical output on just one row (representing one column):

is_hidden                    | 0
column_ordinal               | 1
name                         | AlbumName
is_nullable                  | 0
system_type_id               | 231
system_type_name             | nvarchar(255)
max_length                   | 510
precision                    | 0
scale                        | 0
collation_name               | SQL_Latin1_General_CP1_CI_AS
user_type_id                 | NULL
user_type_database           | NULL
user_type_schema             | NULL
user_type_name               | NULL
assembly_qualified_type_name | NULL
xml_collection_id            | NULL
xml_collection_database      | NULL
xml_collection_schema        | NULL
xml_collection_name          | NULL
is_xml_document              | 0
is_case_sensitive            | 0
is_fixed_length_clr_type     | 0
source_server                | Homer
source_database              | Music
source_schema                | dbo
source_table                 | Albums
source_column                | AlbumName
is_identity_column           | 0
is_part_of_unique_key        | 0
is_updateable                | 1
is_computed_column           | 0
is_sparse_column_set         | 0
ordinal_in_order_by_list     | NULL
order_by_is_descending       | NULL
order_by_list_length         | NULL
error_number                 | NULL
error_severity               | NULL
error_state                  | NULL
error_message                | NULL
error_type                   | NULL
error_type_desc              | NULL

We can see that the source_server, source_database, source_schema, source_table, and source_column columns now provide information about the underlying server, database, schema, table and columns referenced in the view.

In my case, my view queries tables across a linked server. Therefore, the base tables are in a different database, on a different server.

In contrast, when we set @include_browse_information to 2 in the next example, we’ll get the actual columns in the view.

@include_browse_information = 2

Finally, let’s set @include_browse_information to 2.

SELECT * 
FROM sys.dm_exec_describe_first_result_set(
    N'SELECT AlbumName FROM vAlbums', 
    null, 
    2
);

In this case, the query is analysed as if it would be used in preparing or executing a cursor.

This time, only two rows are returned:

+-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
| is_hidden   | column_ordinal   | name      | is_nullable   | system_type_id   | system_type_name   | max_length   | precision   | scale   | collation_name               | user_type_id   | user_type_database   | user_type_schema   | user_type_name   | assembly_qualified_type_name   | xml_collection_id   | xml_collection_database   | xml_collection_schema   | xml_collection_name   | is_xml_document   | is_case_sensitive   | is_fixed_length_clr_type   | source_server   | source_database   | source_schema   | source_table   | source_column   | is_identity_column   | is_part_of_unique_key   | is_updateable   | is_computed_column   | is_sparse_column_set   | ordinal_in_order_by_list   | order_by_is_descending   | order_by_list_length   | error_number   | error_severity   | error_state   | error_message   | error_type   | error_type_desc   |
|-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------|
| 0           | 1                | AlbumName | 0             | 231              | nvarchar(255)      | 510          | 0           | 0       | SQL_Latin1_General_CP1_CI_AS | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | NULL            | Test              | dbo             | vAlbums        | AlbumName       | 0                    | 0                       | 1               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
| 1           | 2                | ROWSTAT^@   | 0             | 56               | int                | 4            | 10          | 0       | NULL                         | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | NULL            | NULL              | NULL            | NULL           | NULL            | 0                    | 0                       | 0               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
+-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

And to save you from scrolling sideways, here’s the first row in vertical output:

is_hidden                    | 0
column_ordinal               | 1
name                         | AlbumName
is_nullable                  | 0
system_type_id               | 231
system_type_name             | nvarchar(255)
max_length                   | 510
precision                    | 0
scale                        | 0
collation_name               | SQL_Latin1_General_CP1_CI_AS
user_type_id                 | NULL
user_type_database           | NULL
user_type_schema             | NULL
user_type_name               | NULL
assembly_qualified_type_name | NULL
xml_collection_id            | NULL
xml_collection_database      | NULL
xml_collection_schema        | NULL
xml_collection_name          | NULL
is_xml_document              | 0
is_case_sensitive            | 0
is_fixed_length_clr_type     | 0
source_server                | NULL
source_database              | Test
source_schema                | dbo
source_table                 | vAlbums
source_column                | AlbumName
is_identity_column           | 0
is_part_of_unique_key        | 0
is_updateable                | 1
is_computed_column           | 0
is_sparse_column_set         | 0
ordinal_in_order_by_list     | NULL
order_by_is_descending       | NULL
order_by_list_length         | NULL
error_number                 | NULL
error_severity               | NULL
error_state                  | NULL
error_message                | NULL
error_type                   | NULL
error_type_desc              | NULL

In this example, the source_table column contains the actual view name, rather than the base table, and the source_database is the name of the local database that I queried the view from. Also, the source_column is the column that I queried from the view, rather than any column from the base tables.

The @params Argument

So far, the only thing we’ve used the @params argument for is to set it to NULL.

If the SQL batch that you’re analysing contains parameters, use the @params function to declare those parameters.

This works similar to the way you declare parameters when using the sp_executesql procedure.

Here’s an example that declares a parameter with the @params argument.

SELECT * 
FROM sys.dm_exec_describe_first_result_set(
    N'SELECT ArtistName FROM Homer.Music.dbo.Artists WHERE ArtistId = @ArtistId', 
    N'@ArtistId int', 
    1
);

Result (using vertical output):

-[ RECORD 1 ]-------------------------
is_hidden                    | 0
column_ordinal               | 1
name                         | ArtistName
is_nullable                  | 0
system_type_id               | 231
system_type_name             | nvarchar(255)
max_length                   | 510
precision                    | 0
scale                        | 0
collation_name               | SQL_Latin1_General_CP1_CI_AS
user_type_id                 | NULL
user_type_database           | NULL
user_type_schema             | NULL
user_type_name               | NULL
assembly_qualified_type_name | NULL
xml_collection_id            | NULL
xml_collection_database      | NULL
xml_collection_schema        | NULL
xml_collection_name          | NULL
is_xml_document              | 0
is_case_sensitive            | 0
is_fixed_length_clr_type     | 0
source_server                | Homer
source_database              | Music
source_schema                | dbo
source_table                 | Artists
source_column                | ArtistName
is_identity_column           | 0
is_part_of_unique_key        | 0
is_updateable                | 1
is_computed_column           | 0
is_sparse_column_set         | 0
ordinal_in_order_by_list     | NULL
order_by_is_descending       | NULL
order_by_list_length         | NULL
error_number                 | NULL
error_severity               | NULL
error_state                  | NULL
error_message                | NULL
error_type                   | NULL
error_type_desc              | NULL
-[ RECORD 2 ]-------------------------
is_hidden                    | 1
column_ordinal               | 2
name                         | ArtistId
is_nullable                  | 0
system_type_id               | 56
system_type_name             | int
max_length                   | 4
precision                    | 10
scale                        | 0
collation_name               | NULL
user_type_id                 | NULL
user_type_database           | NULL
user_type_schema             | NULL
user_type_name               | NULL
assembly_qualified_type_name | NULL
xml_collection_id            | NULL
xml_collection_database      | NULL
xml_collection_schema        | NULL
xml_collection_name          | NULL
is_xml_document              | 0
is_case_sensitive            | 0
is_fixed_length_clr_type     | 0
source_server                | Homer
source_database              | Music
source_schema                | dbo
source_table                 | Artists
source_column                | ArtistId
is_identity_column           | 0
is_part_of_unique_key        | 1
is_updateable                | 1
is_computed_column           | 0
is_sparse_column_set         | 0
ordinal_in_order_by_list     | NULL
order_by_is_descending       | NULL
order_by_list_length         | NULL
error_number                 | NULL
error_severity               | NULL
error_state                  | NULL
error_message                | NULL
error_type                   | NULL
error_type_desc              | NULL