Sqlserver
 SQL >> Database >  >> RDS >> Sqlserver

Come funziona sys.dm_exec_describe_first_result_set_for_object in SQL Server

In SQL Server, la funzione di gestione dinamica sys.dm_exec_describe_first_result_set_for_object restituisce i metadati che descrivono il primo result set di un determinato modulo.

Le passi un @object_id e lei riporta i metadati del primo result set per il modulo che porta quell'ID.

Applica lo stesso algoritmo della stored procedure sp_describe_first_result_set e della funzione sys.dm_exec_describe_first_result_set e, di fatto, svolge lo stesso compito: la differenza è che è limitata alle stored procedure e ai trigger.

Se le passi l'ID di qualunque altro tipo di oggetto (una vista, una funzione, una tabella e così via) riceverai un errore.

Sintassi

La sintassi è questa:

sys.dm_exec_describe_first_result_set_for_object ( @object_id , @include_browse_information )

Qui @object_id è l'ID della stored procedure o del trigger, mentre @include_browse_information ti permette di lavorare in modalità browse come se la query avesse allegata l'opzione FOR BROWSE.

Un esempio

Ecco una dimostrazione.

In questo caso uso la funzione OBJECT_ID() per recuperare l'ID della stored procedure, evitandomi di dover conoscere il numero a memoria.

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

Risultato:

+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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                | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------|------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

Questa funzione restituisce un gran numero di colonne; la documentazione Microsoft spiega ciascuna di esse in dettaglio.

Ecco di nuovo quella prima riga, questa volta in output verticale:

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

Nota che le colonne source_server, source_database, source_schema, source_table e source_column tornano come NULL. Non saranno NULL nei prossimi due esempi.

Modalità Browse

Nell'esempio precedente ho passato 0 come secondo argomento. Puoi usare 0, 1 oppure 2.

Ecco cosa fa ciascun valore:

ValoreRisultato
0Non viene restituita alcuna informazione.
1Ogni query viene analizzata come se includesse l'opzione FOR BROWSE. In questo caso vengono restituiti i nomi delle tabelle di base come informazione sulle colonne di origine.
2Ogni query viene analizzata come se dovesse essere usata per preparare o eseguire un cursore. In questo caso vengono restituiti i nomi delle viste come informazione sulle colonne di origine.

Il caso 0 l'abbiamo già visto. Nei prossimi due esempi lo imposterò quindi a 1 e poi a 2.

Vale la pena segnalarlo: la documentazione Microsoft descrive questo argomento come tipo bit e menziona solo due valori possibili, 0 e 1. Eppure, quando ho testato la funzione sia su SQL Server 2017 sia su SQL Server 2019, si tratta in realtà di un tinyint.

Oltre a ciò, la documentazione della stored procedure sp_describe_first_result_set afferma esplicitamente che questo argomento è un tinyint che accetta i tre valori sopra indicati.

Inoltre, la documentazione di sys.dm_exec_describe_first_result_set_for_object dice che si basa sullo stesso algoritmo di sp_describe_first_result_set.

In ogni caso, gli esempi qui sotto coprono tutti e tre i valori.

@include_browse_information = 1

In questo esempio imposto @include_browse_information a 1.

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

Risultato:

+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------|------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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                | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------|------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

Questa volta tornano quattro righe, che rappresentano quattro colonne ricavate dalle tabelle sottostanti.

Estraiamo la prima colonna usando di nuovo l'output verticale, come prima:

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

Ora le colonne source_server, source_database, source_schema, source_table e source_column sono popolate.

Queste colonne ora riflettono il server, il database, lo schema, la tabella e la colonna di origine della query contenuta nella stored procedure.

Per chiarire, ecco la definizione effettiva della stored procedure:

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

La procedura si basa sulla denominazione in quattro parti per fare riferimento a un server collegato chiamato "Homer" insieme al database, allo schema, alle tabelle e alle colonne del server remoto — cosa che corrisponde esattamente a ciò che restituisce la funzione sys.dm_exec_describe_first_result_set_for_object.

@include_browse_information = 2

In questo esempio imposto @include_browse_information a 2.

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

Risultato:

+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------|------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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                | 0                 | 0                       | 0                          | NULL                | NULL                | NULL                | NULL           | NULL                | 0                    | 0                     | 0                    | 0                    | 0                  | NULL                       | NULL                       | NULL                 | NULL           | NULL                     | NULL                | NULL                | NULL           | NULL                | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------|------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

Questa volta vengono restituite solo tre righe, che rappresentano tre colonne, una delle quali è una colonna ROWSTAT.