Di SQL Server, sys.dm_exec_describe_first_result_set fungsi manajemen dinamis mengembalikan metadata dari kumpulan hasil pertama untuk pernyataan atau pernyataan T-SQL yang diberikan.
Fungsi ini menggunakan algoritma yang sama dengan sp_describe_first_result_set prosedur tersimpan sistem, dan melakukan hal yang hampir sama.
Ia menerima tiga parameter, yang pertama adalah pernyataan T-SQL yang Anda analisis.
Sintaks
Sintaksnya seperti ini:
sys.dm_exec_describe_first_result_set(@tsql, @params, @include_browse_information) Contoh
Berikut ini contoh untuk didemonstrasikan.
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT * FROM Artists',
null,
0
); Hasil:
+-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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 | +-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Fungsi ini mengembalikan beberapa kolom, jadi Anda mungkin perlu menggulir ke samping untuk melihat semuanya. Silakan lihat dokumentasi Microsoft untuk penjelasan setiap kolom yang dikembalikan.
Mode Jelajah
Argumen ketiga – @include_browse_information – menentukan apakah kolom kunci tambahan dan informasi tabel sumber dikembalikan.
Nilai berikut diterima untuk argumen ini:
| Nilai | Hasil |
|---|---|
0 | Tidak ada informasi yang dikembalikan. |
1 | Setiap kueri dianalisis seolah-olah menyertakan FOR BROWSE pilihan pada kueri. Ini akan mengembalikan nama tabel dasar sebagai informasi kolom sumber. |
2 | Setiap kueri dianalisis seolah-olah akan digunakan dalam menyiapkan atau mengeksekusi kursor. Ini akan mengembalikan nama tampilan sebagai informasi kolom sumber. |
Di bawah ini adalah contoh yang menggambarkan bagaimana masing-masing nilai ini memengaruhi hasil.
Untuk membuat semuanya lebih ringkas, saya akan memodifikasi pernyataan T-SQL saya untuk mengembalikan hanya satu kolom.
Saya harus menyebutkan bahwa dokumentasi Microsoft untuk sys.dm_exec_describe_first_result_set hanya mengacu pada dua nilai saja:0 dan 1 . Namun, dokumentasi untuk sp_describe_first_result_set menentukan tiga nilai:0 , 1 , dan 2 , seperti yang tercantum dalam tabel di atas.
Dokumentasi untuk sys.dm_exec_describe_first_result_set juga menyatakan bahwa ia menggunakan algoritma yang sama dengan sp_describe_first_result_set .
Saya juga memeriksa fungsi ini di SQL Server 2017 dan SQL Server 2019, dan parameter ini sebenarnya adalah tinyint , yang menunjukkan bahwa itu dirancang untuk menerima lebih dari dua nilai.
Apa pun itu, contoh berikut menyertakan ketiga nilai tersebut.
@include_browse_information = 0
Dalam contoh ini saya mengatur @include_browse_information ke 0 .
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT AlbumName FROM vAlbums',
null,
0
); Hasil (menggunakan keluaran vertikal):
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
Perhatikan bahwa beberapa kolom adalah NULL . Secara khusus, perhatikan bahwa source_database , source_schema , source_table , dan source_column kolomnya adalah NULL .
Kolom ini tidak boleh NULL dalam dua contoh berikutnya, namun mereka akan menghasilkan dua hasil yang berbeda.
@include_browse_information = 1
Dalam contoh ini saya mengatur @include_browse_information ke 1 .
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT AlbumName FROM vAlbums',
null,
1
);
Menyetel @include_browse_information ke 1 mengembalikan hasilnya seolah-olah itu termasuk FOR BROWSE pilihan pada kueri.
Dalam kasus kami, ini sekarang menghasilkan empat baris yang dikembalikan (mewakili empat kolom dari tabel dasar).
Berikut adalah empat baris yang dikembalikan:
+-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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 | +-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Jadi meskipun saya hanya menentukan satu kolom dalam kueri T-SQL yang saya berikan ke prosedur, itu mengembalikan info untuk empat kolom. Ini adalah empat kolom yang dirujuk oleh vAlbums lihat.
Jika Anda menggulir ke samping cukup jauh, Anda akan melihat bahwa source_database , source_schema , source_table , dan source_column kolom tidak lagi NULL . Juga bukan source_server kolom. Mereka memberikan informasi tentang objek dasar yang ditanyakan oleh tampilan.
Agar lebih mudah dilihat, mari kita gunakan output vertikal hanya pada satu baris (mewakili satu kolom):
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
Kita dapat melihat bahwa source_server , source_database , source_schema , source_table , dan source_column kolom sekarang memberikan informasi tentang server yang mendasarinya, database, skema, tabel, dan kolom yang direferensikan dalam tampilan.
Dalam kasus saya, tampilan saya menanyakan tabel di server yang ditautkan. Oleh karena itu, tabel dasar berada di database yang berbeda, di server yang berbeda.
Sebaliknya, saat kita menyetel @include_browse_information ke 2 dalam contoh berikutnya, kita akan mendapatkan kolom sebenarnya dalam tampilan.
@include_browse_information = 2
Terakhir, mari kita atur @include_browse_information ke 2 .
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT AlbumName FROM vAlbums',
null,
2
); Dalam hal ini, kueri dianalisis seolah-olah akan digunakan dalam menyiapkan atau mengeksekusi kursor.
Kali ini, hanya dua baris yang dikembalikan:
+-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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 | +-------------+------------------+-----------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Dan untuk menyelamatkan Anda dari menggulir ke samping, inilah baris pertama dalam output vertikal:
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
Dalam contoh ini, source_table kolom berisi nama tampilan yang sebenarnya, bukan tabel dasar, dan source_database adalah nama database lokal yang saya tanyakan tampilannya. Juga, source_column adalah kolom yang saya tanyakan dari tampilan, bukan kolom apa pun dari tabel dasar.
@params Argumen
Sejauh ini, satu-satunya hal yang kami gunakan adalah @params argumen untuk adalah mengaturnya ke NULL .
Jika kumpulan SQL yang Anda analisis berisi parameter, gunakan @params berfungsi untuk mendeklarasikan parameter tersebut.
Ini bekerja mirip dengan cara Anda mendeklarasikan parameter saat menggunakan sp_executesql prosedur.
Berikut adalah contoh yang mendeklarasikan parameter dengan @params argumen.
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT ArtistName FROM Homer.Music.dbo.Artists WHERE ArtistId = @ArtistId',
N'@ArtistId int',
1
); Hasil (menggunakan keluaran vertikal):
-[ 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