Terkadang saat Anda menjalankan kueri di SQL Server, Anda mungkin ingin mengetahui tipe data yang mendasari setiap kolom, presisi, panjangnya, apakah nullable atau tidak, dll.
Jika Anda hanya menanyakan satu tabel, Anda bisa mendapatkan data semacam ini dengan prosedur seperti sp_columns
. Tetapi jika kueri Anda berjalan di banyak tabel, ini bisa menjadi sangat berat dengan sangat cepat.
Untungnya ada beberapa cara yang dapat Anda lakukan untuk mendapatkan metadata tersebut untuk hasil yang ditetapkan di SQL Server.
sp_describe_first_result_set
Prosedur Tersimpan Sistem
sp_describe_first_result_set
prosedur tersimpan sistem dirancang khusus untuk mengembalikan metadata untuk kumpulan hasil.
Ini menerima tiga parameter, tetapi dua yang terakhir adalah opsional. Parameter pertama adalah kueri T-SQL yang ingin Anda analisis.
Berikut adalah contoh dari prosedur ini dalam tindakan.
DECLARE @tsql_query nvarchar(max);
SET @tsql_query = 'SELECT
ar.ArtistName,
al.AlbumName,
g.Genre
FROM
Artists ar
INNER JOIN Albums al
ON ar.ArtistId = al.ArtistId
INNER JOIN Homer.Music.dbo.Genres g
ON al.GenreId = g.GenreId';
EXEC sp_describe_first_result_set @tsql_query, null, 1;
Anda sebenarnya dapat memberikan kueri sebagai string literal, tetapi dalam kasus ini saya telah memasukkannya ke dalam variabel bernama @tsql_query
.
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 | tds_type_id | tds_length | tds_collation_id | tds_collation_sort_id | |-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------| | 0 | 1 | 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 | Music | dbo | Artists | ArtistName | 0 | 0 | 1 | 0 | 0 | NULL | NULL | NULL | 231 | 510 | 13632521 | 52 | | 0 | 2 | 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 | | 0 | 3 | Genre | 0 | 231 | nvarchar(50) | 100 | 0 | 0 | SQL_Latin1_General_CP1_CI_AS | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 0 | 0 | 0 | Homer | Music | dbo | Genres | Genre | 0 | 0 | 1 | 0 | 0 | NULL | NULL | NULL | 231 | 100 | 13632521 | 52 | | 1 | 4 | 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 | 5 | 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 | 6 | 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 | 56 | 4 | NULL | NULL | +-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------+
Seperti yang Anda lihat, prosedur tersimpan ini mengembalikan banyak kolom.
Jika Anda melihat dengan cermat kueri yang saya analisis, Anda akan melihat bahwa salah satu tabel yang dirujuk ada di server tertaut yang disebut "Homer". Dalam hasil yang dihasilkan oleh sp_describe_first_result_set
, nilai di source_server
kolom mencerminkan server tertaut ini untuk kolom yang mendasarinya.
Dalam contoh ini, saya menggunakan 1
sebagai argumen ketiga. Anda juga dapat menggunakan 0
atau 2
. Lihat Bagaimana sp_describe_first_result_set
Berfungsi untuk penjelasan dan contoh bagaimana setelan ini memengaruhi hasil.
sys.dm_exec_describe_first_result_set
Tampilan Sistem
sys.dm_exec_describe_first_result_set
tampilan sistem menggunakan algoritme yang sama dengan sp_describe_first_result_set
, dan mengembalikan informasi yang sama.
Jadi kita bisa memodifikasi contoh sebelumnya untuk menggunakan sys.dm_exec_describe_first_result_set
bukannya sp_describe_first_result_set
.
DECLARE @tsql_query nvarchar(max);
SET @tsql_query = 'SELECT
ar.ArtistName,
al.AlbumName,
g.Genre
FROM
Artists ar
INNER JOIN Albums al
ON ar.ArtistId = al.ArtistId
INNER JOIN Homer.Music.dbo.Genres g
ON al.GenreId = g.GenreId';
SELECT *
FROM sys.dm_exec_describe_first_result_set(
@tsql_query,
null,
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 | 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 | Music | dbo | Artists | ArtistName | 0 | 0 | 1 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | | 0 | 2 | 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 | NULL | NULL | NULL | NULL | NULL | NULL | | 0 | 3 | Genre | 0 | 231 | nvarchar(50) | 100 | 0 | 0 | SQL_Latin1_General_CP1_CI_AS | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 0 | 0 | 0 | Homer | Music | dbo | Genres | Genre | 0 | 0 | 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 | NULL | Music | dbo | Artists | ArtistId | 1 | 1 | 0 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | | 1 | 5 | 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 | NULL | NULL | NULL | NULL | NULL | NULL | | 1 | 6 | 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 | +-------------+------------------+------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Seperti sp_describe_first_result_set
, Anda dapat mengubah argumen kedua dan ketiga. Lihat Cara Kerja sys.dm_exec_describe_first_result_set untuk contoh.
Menggunakan OPENROWSET
Jika Anda melihat kode sumber untuk sys.dm_exec_describe_first_result_set
, Anda akan melihat bahwa ia menggunakan OPENROWSET
untuk mencapai hasilnya.
Demikian pula, kita dapat menggunakan OPENROWSET
untuk melakukan hal serupa. Misalnya, kita bisa menggunakan OPENROWSET
untuk menjalankan hasil nol teratas dari kueri ke dalam tabel sementara, lalu gunakan sp_columns
prosedur untuk mengembalikan informasi kolom tentang tabel sementara itu (yang mencerminkan kumpulan hasil kueri kami).
SELECT TOP 0 * INTO #TempTable
FROM OPENROWSET(
'SQLNCLI',
'Server=localhost;Trusted_Connection=yes;',
'SELECT
ar.ArtistName,
al.AlbumName,
g.Genre
FROM
Music.dbo.Artists ar
INNER JOIN Music.dbo.Albums al
ON ar.ArtistId = al.ArtistId
INNER JOIN Music.dbo.Genres g
ON al.GenreId = g.GenreId');
EXEC('USE tempdb EXEC sp_columns #TempTable DROP TABLE #TempTable');
Hasil:
+-------------------+---------------+----------------------------------------------------------------------------------------------------------------------------------+---------------+-------------+-------------+-------------+----------+---------+---------+------------+-----------+--------------+-----------------+--------------------+---------------------+--------------------+---------------+----------------+ | TABLE_QUALIFIER | TABLE_OWNER | TABLE_NAME | COLUMN_NAME | DATA_TYPE | TYPE_NAME | PRECISION | LENGTH | SCALE | RADIX | NULLABLE | REMARKS | COLUMN_DEF | SQL_DATA_TYPE | SQL_DATETIME_SUB | CHAR_OCTET_LENGTH | ORDINAL_POSITION | IS_NULLABLE | SS_DATA_TYPE | |-------------------+---------------+----------------------------------------------------------------------------------------------------------------------------------+---------------+-------------+-------------+-------------+----------+---------+---------+------------+-----------+--------------+-----------------+--------------------+---------------------+--------------------+---------------+----------------| | tempdb | dbo | #TempTable__________________________________________________________________________________________________________00000000000C | ArtistName | -9 | nvarchar | 255 | 510 | NULL | NULL | 0 | NULL | NULL | -9 | NULL | 510 | 1 | NO | 39 | | tempdb | dbo | #TempTable__________________________________________________________________________________________________________00000000000C | AlbumName | -9 | nvarchar | 255 | 510 | NULL | NULL | 0 | NULL | NULL | -9 | NULL | 510 | 2 | NO | 39 | | tempdb | dbo | #TempTable__________________________________________________________________________________________________________00000000000C | Genre | -9 | nvarchar | 50 | 100 | NULL | NULL | 0 | NULL | NULL | -9 | NULL | 100 | 3 | NO | 39 | +-------------------+---------------+----------------------------------------------------------------------------------------------------------------------------------+---------------+-------------+-------------+-------------+----------+---------+---------+------------+-----------+--------------+-----------------+--------------------+---------------------+--------------------+---------------+----------------+
Anda dapat mengganti sp_columns
dengan sp_help
jika disukai.
Saya pikir dua metode pertama lebih baik, tetapi setidaknya ini adalah opsi lain (terutama jika Anda menggunakan versi SQL Server yang lebih lama yang tidak mendukung sys.dm_exec_describe_first_result_set
atau sp_describe_first_result_set
.