Di SQL Server, sys.dm_exec_describe_first_result_set_for_object
fungsi manajemen dinamis mengembalikan metadata dari hasil pertama yang ditetapkan untuk modul yang diberikan.
Dibutuhkan @object_id
sebagai parameter dan menjelaskan metadata hasil pertama untuk modul dengan ID tersebut.
Ini menggunakan algoritma yang sama dengan sp_describe_first_result_set
prosedur tersimpan sistem dan sys.dm_exec_describe_first_result_set
berfungsi, dan melakukan hal yang hampir sama, kecuali bahwa itu terbatas pada prosedur dan pemicu yang tersimpan saja.
Jika Anda meneruskan ID dari jenis objek yang berbeda (seperti tampilan, fungsi, tabel, dll) maka itu akan mengembalikan kesalahan.
Sintaks
Sintaksnya seperti ini:
sys.dm_exec_describe_first_result_set_for_object
( @object_id , @include_browse_information )
Dimana @object_id
adalah ID prosedur atau pemicu tersimpan, dan @include_browse_information
memungkinkan Anda untuk menggunakan mode jelajah seolah-olah ada FOR BROWSE
pilihan pada kueri.
Contoh
Berikut ini contoh untuk didemonstrasikan.
Dalam contoh ini, saya menggunakan OBJECT_ID()
berfungsi untuk mengembalikan ID prosedur tersimpan (yang menyelamatkan saya dari keharusan mengetahui ID sebenarnya).
SELECT *
FROM sys.dm_exec_describe_first_result_set_for_object(
OBJECT_ID('spAlbumsFromArtist'),
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 | 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 | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Fungsi ini mengembalikan banyak kolom. Lihat dokumentasi Microsoft untuk penjelasan lengkap setiap kolom.
Ini baris pertama lagi, tetapi menggunakan 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 | 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 source_server
, source_database
, source_schema
, source_table
, dan source_column
kolomnya adalah NULL
. Mereka tidak akan menjadi NULL
dalam dua contoh berikutnya.
Mode Jelajah
Pada contoh sebelumnya, saya menggunakan 0
untuk argumen kedua. Anda dapat mengatur ini ke 0
, 1
, atau 2
.
Berikut arti setiap nilai:
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. |
Kita telah melihat apa yang terjadi jika disetel ke 0
. Jadi dalam dua contoh berikutnya, saya akan menyetelnya ke 1
dan 2
masing-masing.
Saya harus menunjukkan bahwa dokumentasi Microsoft menyatakan bahwa argumen ini bertipe bit , dan hanya mengacu pada dua kemungkinan nilai:0
dan 1
. Namun, saya memeriksa fungsi ini di SQL Server 2017 dan SQL Server 2019, dan sebenarnya ini adalah tinyint .
Selanjutnya, dokumentasi untuk sp_describe_first_result_set
prosedur tersimpan secara eksplisit menetapkan bahwa argumen ini tinyint , dan menerima tiga nilai yang disebutkan di atas.
Juga, dokumentasi untuk sys.dm_exec_describe_first_result_set_for_object
menyatakan bahwa ia menggunakan algoritma yang sama dengan sp_describe_first_result_set
.
Bagaimanapun, contoh dalam artikel ini mencakup ketiga nilai tersebut.
@include_browse_information = 1
Dalam contoh ini saya mengatur @include_browse_information
ke 1
.
SELECT *
FROM sys.dm_exec_describe_first_result_set_for_object(
OBJECT_ID('spAlbumsFromArtist'),
1
);
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 | 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 | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Kali ini empat baris dikembalikan, mewakili empat kolom dari tabel yang mendasarinya.
Mari kita pilih kolom pertama menggunakan output vertikal lagi, seperti yang kita lakukan pada contoh sebelumnya:
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
Kali ini source_server
, source_database
, source_schema
, source_table
, dan source_column
kolom tidak lagi NULL
.
Dalam hal ini, kolom tersebut mencerminkan server yang mendasari, database, skema, tabel, dan kolom kueri dalam prosedur tersimpan.
Untuk mendemonstrasikan apa yang saya maksud, inilah definisi prosedur tersimpan yang sebenarnya:
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
Prosedur tersimpan menggunakan empat bagian penamaan untuk merujuk server tertaut yang disebut "Homer", serta database, skema, tabel, dan kolom dari server jauh. Ini cocok dengan data yang dikembalikan oleh sys.dm_exec_describe_first_result_set_for_object
fungsi.
@include_browse_information = 2
Dalam contoh ini saya mengatur @include_browse_information
ke 2
.
SELECT *
FROM sys.dm_exec_describe_first_result_set_for_object(
OBJECT_ID('spAlbumsFromArtist'),
2
);
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 | 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 | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Kali ini hanya tiga baris yang dikembalikan, mewakili tiga kolom, termasuk ROWSTAT
kolom.