How sys.dm_exec_describe_first_result_set_for_object Works in SQL Server

In SQL Server, the sys.dm_exec_describe_first_result_set_for_object dynamic management function returns the metadata of the first result set for a given module.

It takes an @object_id as a parameter and describes the first result metadata for the module with that ID.

It uses the same algorithm as the sp_describe_first_result_set system stored procedure and the sys.dm_exec_describe_first_result_set function, and does pretty much the same thing, except that it’s limited to just stored procedures and triggers.

If you pass the ID of a different object type (such as a view, function, table, etc) then it will return an error.

Syntax

The syntax goes like this:

sys.dm_exec_describe_first_result_set_for_object   
    ( @object_id , @include_browse_information )

Where @object_id is the ID of the stored procedure or trigger, and @include_browse_information allows you to use browse mode as if there’s a FOR BROWSE option on the query.

Example

Here’s an example to demonstrate.

In this example, I use the OBJECT_ID() function to return the ID of the stored procedure (which saves me from having to know the actual ID).

SELECT * 
FROM sys.dm_exec_describe_first_result_set_for_object(
    OBJECT_ID('spAlbumsFromArtist'),
    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                | 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            | NULL              | NULL            | NULL           | NULL            | 0                    | NULL                    | 1               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
| 0           | 2                | ReleaseDate | 0             | 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 a lot of columns. See Microsoft’s documentation for a full run down of each column.

Here’s the first row again, but 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

Take note that the the source_server, source_database, source_schema, source_table, and source_column columns are NULL. They won’t be NULL in the next two examples.

Browse Mode

In the previous example, I used 0 for the second argument. You can set this to 0, 1, or 2.

Here’s what each value means:

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.

We’ve already see what happens when it’s set to 0. So in the next two examples, I’ll set it to 1 and 2 respectively.

I should point out that the Microsoft documentation states that this argument is type bit, and only refers to two possible values: 0 and 1. However, I checked this function in both SQL Server 2017 and SQL Server 2019, and it is actually a tinyint.

Further to this, the documentation for the sp_describe_first_result_set stored procedure explicitly specifies that this argument is tinyint, and that it accepts the three values mentioned above.

Also, the documentation for sys.dm_exec_describe_first_result_set_for_object states that it uses the same algorithm as sp_describe_first_result_set.

In any case, the examples in this article include all three values.

@include_browse_information = 1

In this example I set @include_browse_information to 1.

SELECT * 
FROM sys.dm_exec_describe_first_result_set_for_object(
    OBJECT_ID('spAlbumsFromArtist'),
    1
);

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                | 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              |
| 0           | 2                | ReleaseDate | 0             | 40               | date               | 3            | 10          | 0       | NULL                         | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | Homer           | Music             | dbo             | Albums         | ReleaseDate     | 0                    | 0                       | 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                | 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              |
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

This time four rows are returned, representing four columns from the underlying tables.

Let’s single out the first column using vertical output again, like we did in the previous example:

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

This time the source_server, source_database, source_schema, source_table, and source_column columns are no longer NULL.

In this case, those columns reflect the underlying server, database, schema, table, and column of the query within the stored procedure.

To demonstrate what I mean, here’s the actual stored procedure definition:

CREATE PROCEDURE [dbo].[spAlbumsFromArtist] 
	@ArtistName varchar(255)
AS
	SELECT AlbumName, ReleaseDate
	FROM Homer.Music.dbo.Albums al
		INNER JOIN Homer.Music.dbo.Artists ar
		ON al.ArtistId = ar.ArtistId 
	WHERE ar.ArtistName = @ArtistName;
GO

The stored procedure uses four part naming to reference a linked server called “Homer”, as well as the database, schema, tables and columns from the remote server. This matches with the data returned by the sys.dm_exec_describe_first_result_set_for_object function.

@include_browse_information = 2

In this example I set @include_browse_information to 2.

SELECT * 
FROM sys.dm_exec_describe_first_result_set_for_object(
    OBJECT_ID('spAlbumsFromArtist'),
    2
);

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                | 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              |
| 0           | 2                | ReleaseDate | 0             | 40               | date               | 3            | 10          | 0       | NULL                         | NULL           | NULL                 | NULL               | NULL             | NULL                           | NULL                | NULL                      | NULL                    | NULL                  | 0                 | 0                   | 0                          | Homer           | Music             | dbo             | Albums         | ReleaseDate     | 0                    | 0                       | 1               | 0                    | 0                      | NULL                       | NULL                     | NULL                   | NULL           | NULL             | NULL          | NULL            | NULL         | NULL              |
| 1           | 3                | 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              |
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

This time only three rows are returned, representing three columns, including a ROWSTAT column.