I SQL Server er sys.dm_exec_describe_first_result_set
dynamisk styringsfunktion returnerer metadataene for det første resultatsæt for en given T-SQL-sætning eller sætninger.
Denne funktion bruger den samme algoritme som sp_describe_first_result_set
systemlagret procedure, og gør stort set det samme.
Den accepterer tre parametre, hvoraf den første er de T-SQL-sætninger, du analyserer.
Syntaks
Syntaksen ser sådan ud:
sys.dm_exec_describe_first_result_set(@tsql, @params, @include_browse_information)
Eksempel
Her er et eksempel til at demonstrere.
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT * FROM Artists',
null,
0
);
Resultat:
+-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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 | +-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Denne funktion returnerer en del kolonner, så du bliver sandsynligvis nødt til at rulle sidelæns for at se dem alle. Du er velkommen til at tjekke Microsofts dokumentation for en forklaring af hver kolonne, der returneres.
Browsetilstand
Det tredje argument – @include_browse_information
– angiver, om yderligere nøglekolonner og kildetabeloplysninger returneres.
Følgende værdier accepteres for dette argument:
Værdi | Resultat |
---|---|
0 | Ingen information returneres. |
1 | Hver forespørgsel analyseres, som om den indeholder en FOR BROWSE mulighed på forespørgslen. Dette vil returnere basistabelnavne som oplysninger om kildekolonnen. |
2 | Hver forespørgsel analyseres, som om den ville blive brugt til at forberede eller udføre en markør. Dette vil returnere visningsnavne som kildekolonneoplysninger. |
Nedenfor er eksempler, der illustrerer, hvordan hver af disse værdier påvirker resultatet.
For at gøre tingene mere kortfattede vil jeg ændre min T-SQL-sætning til kun at returnere én kolonne.
Jeg skal nævne, at Microsoft-dokumentationen for sys.dm_exec_describe_first_result_set
henviser kun til to værdier:0
og 1
. Dog dokumentationen for sp_describe_first_result_set
angiver tre værdier:0
, 1
og 2
, som angivet i ovenstående tabel.
Dokumentationen for sys.dm_exec_describe_first_result_set
angiver også, at den bruger samme algoritme som sp_describe_first_result_set
.
Jeg tjekkede også denne funktion i både SQL Server 2017 og SQL Server 2019, og denne parameter er faktisk en tinyint , hvilket antyder, at den er designet til at acceptere mere end to værdier.
Uanset hvad, inkluderer følgende eksempler alle tre værdier.
@include_browse_information = 0
I dette eksempel indstillede jeg @include_browse_information
til 0
.
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT AlbumName FROM vAlbums',
null,
0
);
Resultat (ved hjælp af lodret 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
Bemærk, at en hel del kolonner er NULL
. Bemærk især, at source_database
, source_schema
, source_table
og source_column
kolonner er NULL
.
Disse kolonner vil ikke være NULL
i de næste to eksempler, men de vil producere to forskellige resultater.
@include_browse_information = 1
I dette eksempel indstillede jeg @include_browse_information
til 1
.
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT AlbumName FROM vAlbums',
null,
1
);
Indstilling af @include_browse_information
til 1
returnerer resultatet, som om det indeholder en FOR BROWSE
mulighed på forespørgslen.
I vores tilfælde resulterer dette nu i, at fire rækker returneres (der repræsenterer fire kolonner fra basistabellerne).
Her er de fire returnerede rækker:
+-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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 | +-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Så selvom jeg kun specificerede én kolonne i T-SQL-forespørgslen, som jeg sendte til proceduren, returnerede den info for fire kolonner. Dette er de fire kolonner, der refereres til af vAlbums
se.
Hvis du ruller sidelæns langt nok, vil du se, at source_database
, source_schema
, source_table
og source_column
kolonner er ikke længere NULL
. Det er source_server
heller ikke kolonne. De giver information om de basisobjekter, som visningen forespørger på.
For at gøre det nemmere at se, lad os bruge lodret output på kun én række (repræsenterer én kolonne):
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
Vi kan se, at source_server
, source_database
, source_schema
, source_table
og source_column
kolonner giver nu information om den underliggende server, database, skema, tabel og kolonner, der henvises til i visningen.
I mit tilfælde forespørger min visning tabeller på tværs af en forbundet server. Derfor er basistabellerne i en anden database, på en anden server.
I modsætning hertil, når vi indstiller @include_browse_information
til 2
i det næste eksempel får vi de faktiske kolonner i visningen.
@include_browse_information = 2
Lad os endelig indstille @include_browse_information
til 2
.
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT AlbumName FROM vAlbums',
null,
2
);
I dette tilfælde analyseres forespørgslen, som om den ville blive brugt til at forberede eller udføre en markør.
Denne gang returneres kun to rækker:
+-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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 | +-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Og for at spare dig for at rulle sidelæns, her er den første række i lodret 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
I dette eksempel er source_table
kolonnen indeholder det faktiske visningsnavn i stedet for basistabellen og source_database
er navnet på den lokale database, som jeg forespurgte visningen fra. Også 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
);
Resultat (ved hjælp af lodret 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