I SQL Server er sp_describe_first_result_set
Systemlagret procedure returnerer metadataene for et resultatsæt.
Mere specifikt returnerer den metadataene for det første mulige resultatsæt af en T-SQL-batch.
Den accepterer tre parametre, hvoraf den første er de T-SQL-sætninger, du analyserer.
Eksempel
Her er et eksempel til at demonstrere.
EXEC sp_describe_first_result_set
@tsql = N'SELECT * FROM Artists',
@params = null,
@browse_information_mode = 0;
Denne kode navngiver eksplicit hver af de tre parametre, som denne lagrede procedure accepterer.
Kun den første parameter er påkrævet. Som med enhver lagret procedure kan du også udelade parameternavnene, hvis du foretrækker det.
Så vi kunne også bruge følgende kode:
EXEC sp_describe_first_result_set N'SELECT * FROM Artists';
Og her er resultatet:
+-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------+ | 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 | tds_type_id | tds_length | tds_collation_id | tds_collation_sort_id | |-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------| | 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 | 56 | 4 | 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 | 231 | 510 | 13632521 | 52 | | 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 | 40 | 3 | NULL | NULL | +-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------+
Der er mange kolonner, der returneres med denne lagrede procedure (se Microsofts dokumenter for en forklaring af hver), så du bliver sandsynligvis nødt til at rulle sidelæns for at se dem alle.
Jeg skifter til lodret output i nogle af mine næste eksempler, hvis du har problemer med at rulle sidelæns.
Browsetilstand
Det tredje argument – @browse_information_mode
– 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 viser også resultaterne ved hjælp af lodret output for at spare dig for at skulle rulle sidelæns.
@browse_information_mode = 0
I dette eksempel indstillede jeg @browse_information_mode
til 0
.
EXEC sp_describe_first_result_set
@tsql = N'SELECT AlbumName FROM vAlbums',
@params = null,
@browse_information_mode = 0;
Som tidligere nævnt kan du alternativt udelade parameternavnene, hvis du foretrækker det. Derfor kunne vi bruge følgende kode som en mere kortfattet måde at gøre det samme på.
EXEC sp_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 tds_type_id | 231 tds_length | 510 tds_collation_id | 13632521 tds_collation_sort_id | 52
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.
@browse_information_mode = 1
I dette eksempel indstillede jeg @browse_information_mode
til 1
.
EXEC sp_describe_first_result_set
N'SELECT AlbumName FROM vAlbums', null, 1;
Indstilling af @browse_information_mode
til 1
returnerer resultatet, som om det indeholder en FOR BROWSE
mulighed på forespørgslen.
I vores tilfælde resulterer dette 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 | tds_type_id | tds_length | tds_collation_id | tds_collation_sort_id | |-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------| | 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 | Music | dbo | Albums | AlbumName | 0 | 0 | 1 | 0 | 0 | NULL | NULL | NULL | 231 | 510 | 13632521 | 52 | | 1 | 2 | ArtistId | 0 | 56 | int | 4 | 10 | 0 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 0 | 0 | 0 | NULL | Music | dbo | Artists | ArtistId | 1 | 1 | 0 | 0 | 0 | NULL | NULL | NULL | 56 | 4 | NULL | NULL | | 1 | 3 | AlbumId | 0 | 56 | int | 4 | 10 | 0 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 0 | 0 | 0 | NULL | Music | dbo | Albums | AlbumId | 1 | 1 | 0 | 0 | 0 | NULL | NULL | NULL | 56 | 4 | NULL | NULL | | 1 | 4 | GenreId | 0 | 56 | int | 4 | 10 | 0 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 0 | 0 | 0 | NULL | Music | dbo | Genres | GenreId | 1 | 1 | 0 | 0 | 0 | NULL | NULL | NULL | 56 | 4 | 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.
Lad os skærpe ind på kun én kolonne 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 | 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 tds_type_id | 231 tds_length | 510 tds_collation_id | 13632521 tds_collation_sort_id | 52
Bemærk, at source_database
, source_schema
, source_table
og source_column
kolonner er ikke længere NULL
. De giver information om de basisobjekter, som visningen forespørger på.
I modsætning hertil, når vi indstiller @browse_information_mode
til 2
i det næste eksempel får vi de faktiske kolonner i visningen.
Bemærk også, at source_server
kolonnen er stadig NULL
. Dette skyldes, at den lokale server er den oprindelige server. Men hvis min visning forespurgte tabeller på en sammenkædet server, ville navnet på den server være i source_server
kolonne.
Se hvordan sys.dm_exec_describe_first_result_set virker for et eksempel. I den artikel bruger jeg en lignende visning som den i ovenstående eksempel, bortset fra at den forespørger i en database på en linket server.
@browse_information_mode = 2
Lad os endelig indstille @browse_information_mode
til 2
.
EXEC sp_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 | tds_type_id | tds_length | tds_collation_id | tds_collation_sort_id | |-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------| | 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 | Music | dbo | vAlbums | AlbumName | 0 | 0 | 1 | 0 | 0 | NULL | NULL | NULL | 231 | 510 | 13632521 | 52 | | 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 | 56 | 4 | 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 | Music 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 tds_type_id | 231 tds_length | 510 tds_collation_id | 13632521 tds_collation_sort_id | 52
I dette eksempel er source_table
kolonnen indeholder det faktiske visningsnavn i stedet for basistabellen.
@params
Argument
Indtil videre har vi ikke brugt @params
argument, andet end at sætte det til NULL
.
Hvis den SQL-batch, du analyserer, indeholder parametre, skal du bruge @params
funktion til at erklære disse parametre.
Dette fungerer på samme måde som den måde, du erklærer parametre på, når du bruger sp_executesql
procedure.
Her er et eksempel, der erklærer en parameter med @params
argument.
EXEC sp_describe_first_result_set
@tsql = N'SELECT AlbumName FROM Albums WHERE ArtistId = @ArtistId',
@params = N'@ArtistId int',
@browse_information_mode = 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 tds_type_id | 231 tds_length | 510 tds_collation_id | 13632521 tds_collation_sort_id | 52