Wenn Sie sich jemals in der Situation befinden, dass Sie gerade dabei sind, eine gespeicherte Prozedur auszuführen, sich aber plötzlich selbst erwischen. Sie fragen sich:„Wie viele Spalten gibt dieses Ding zurück? Welche Tische? Fragt es einen entfernten Server ab?“
Die gute Nachricht ist, dass es mehrere Möglichkeiten gibt, diese Informationen zu erhalten, bevor Sie die Abfrage ausführen. Schauen wir sie uns an.
Der sys.dm_exec_describe_first_result_set_for_object
Funktion
Erste Anlaufstelle ist sys.dm_exec_describe_first_result_set_for_object
dynamische Verwaltungsfunktion des Systems.
Diese Funktion akzeptiert eine @object_id
als Parameter und beschreibt die ersten Ergebnismetadaten für das Modul mit dieser ID.
Es kann für gespeicherte Prozeduren und Trigger verwendet werden.
Hier ist ein Beispiel zur Veranschaulichung. In diesem Fall verwende ich die OBJECT_ID()
Funktion, um die ID der gespeicherten Prozedur zurückzugeben (was mir erspart, die tatsächliche ID zu kennen).
SELECT *
FROM sys.dm_exec_describe_first_result_set_for_object(
OBJECT_ID('spAlbumsFromArtist'),
0
);
Ergebnis:
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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 | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Wenn Sie weit genug zur Seite scrollen, sehen Sie, dass der source_server
, source_database
, source_schema
, source_table
und source_column
Spalten sind NULL
. Das liegt daran, dass wir 0
verwendet haben als drittes Argument.
Umschalten des dritten Arguments auf 1
gibt Informationen über die Basistabellen zurück, auf die in der gespeicherten Prozedur verwiesen wird.
SELECT *
FROM sys.dm_exec_describe_first_result_set_for_object(
OBJECT_ID('spAlbumsFromArtist'),
1
);
Ergebnis:
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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 | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Wenn wir jetzt seitwärts scrollen, können wir sehen, dass der source_server
, source_database
, source_schema
, source_table
und source_column
Spalten enthalten alle Werte.
In diesem Fall fragt die Prozedur Tabellen über einen Verbindungsserver namens „Homer“ ab.
Sie werden auch feststellen, dass jetzt vier Zeilen zurückgegeben werden, anstatt nur zwei, die zurückgegeben wurden, als wir das dritte Argument auf 0
gesetzt haben . Die zwei zusätzlichen Zeilen spiegeln die zugrunde liegenden Spalten wider, die innerhalb der gespeicherten Prozedur abgefragt werden.
Hier ist die Definition der gespeicherten Prozedur:
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;
Obwohl die Prozedur also nur zwei Spalten zurückgibt, muss sie auf andere Spalten verweisen, um ihre Aufgabe abzuschließen.
Siehe Wie sys.dm_exec_describe_first_result_set_for_object
Funktioniert für weitere Beispiele.
Der sys.dm_exec_describe_first_result_set
Anzeigen
Diese Ansicht verwendet denselben Algorithmus wie die vorherige Ansicht und gibt dieselben Informationen zurück. Der Unterschied besteht darin, dass ein T-SQL-Batch und nicht die eigentliche gespeicherte Prozedur analysiert wird.
Dies bedeutet, dass Sie diese Ansicht verwenden können, um Code zu analysieren, der sich nicht unbedingt in einer gespeicherten Prozedur oder einem Trigger befindet. Sie können damit beispielsweise eine Ansicht oder eine Ad-hoc-Abfrage analysieren.
Aber in diesem Artikel geht es um die Analyse gespeicherter Prozeduren, also ist hier ein Beispiel dafür.
SELECT *
FROM sys.dm_exec_describe_first_result_set(
'EXEC spAlbumsFromArtist @ArtistId = ''Iron Maiden''',
null,
1
);
Ergebnis:
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+ | 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 | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
Siehe Wie sys.dm_exec_describe_first_result_set
Funktioniert für weitere Beispiele.
Der sp_describe_first_result_set
Gespeicherte Systemprozedur
Das sp_describe_first_result_set
Die gespeicherte Systemprozedur macht dasselbe (und verwendet denselben Algorithmus). Es akzeptiert einen T-SQL-Batch und gibt die Metadaten für seine erste mögliche Ergebnismenge zurück.
EXEC sp_describe_first_result_set
@tsql = N'EXEC spAlbumsFromArtist @ArtistId = ''Iron Maiden''',
@params = null,
@browse_information_mode = 1;
Ergebnis:
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------+ | 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 | Homer | Music | dbo | Albums | AlbumName | 0 | 0 | 1 | 0 | 0 | NULL | NULL | NULL | 231 | 510 | 13632521 | 52 | | 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 | 40 | 3 | 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 | 56 | 4 | 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 | 56 | 4 | NULL | NULL | +-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------+
Siehe Wie sp_describe_first_result_set
Funktioniert für weitere Beispiele.
OPENROWSET
mit einer temporären Tabelle
Eine weitere Option besteht darin, eine Abfrage zu erstellen, die die Top-Zero-Ergebnisse eines OPENROWSET
enthält Abfrage in eine temporäre Tabelle, dann verwenden Sie eine Prozedur wie sp_columns
um Spalteninformationen über diese temporäre Tabelle (die die Daten aus unserer Ergebnismenge enthält) zurückzugeben.
Dadurch werden Details wie Spaltennamen, Datentypen usw. bereitgestellt.
SELECT TOP 0 * INTO #TempTable
FROM OPENROWSET(
'SQLNCLI',
'Server=localhost;Trusted_Connection=yes;',
'EXEC Test.dbo.sp_BadDogs');
EXEC('USE tempdb EXEC sp_columns #TempTable DROP TABLE #TempTable');
Ergebnis:
+-------------------+---------------+----------------------------------------------------------------------------------------------------------------------------------+---------------+-------------+-------------+-------------+----------+---------+---------+------------+-----------+--------------+-----------------+--------------------+---------------------+--------------------+---------------+----------------+ | 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__________________________________________________________________________________________________________000000000017 | DogId | 4 | int | 10 | 4 | 0 | 10 | 0 | NULL | NULL | 4 | NULL | NULL | 1 | NO | 56 | | tempdb | dbo | #TempTable__________________________________________________________________________________________________________000000000017 | DogName | -9 | nvarchar | 255 | 510 | NULL | NULL | 1 | NULL | NULL | -9 | NULL | 510 | 2 | YES | 39 | | tempdb | dbo | #TempTable__________________________________________________________________________________________________________000000000017 | GoodDog | -7 | bit | 1 | 1 | NULL | NULL | 1 | NULL | NULL | -7 | NULL | NULL | 3 | YES | 50 | +-------------------+---------------+----------------------------------------------------------------------------------------------------------------------------------+---------------+-------------+-------------+-------------+----------+---------+---------+------------+-----------+--------------+-----------------+--------------------+---------------------+--------------------+---------------+----------------+
Mit dieser Methode könnten Sie auch Dinge tun wie sp_columns
austauschen für sp_help
oder ähnliches.