sql >> Database >  >> RDS >> Sqlserver

4 manieren om erachter te komen welke kolommen worden geretourneerd door een opgeslagen procedure in SQL Server

Als u zich ooit in de situatie bevindt waarin u op het punt staat een opgeslagen procedure uit te voeren, maar u plotseling betrapt wordt. Je vraagt ​​je af:"Hoeveel kolommen keert dit ding terug? Welke tafels? Vraagt ​​het een externe server?”

Het goede nieuws is dat er verschillende manieren zijn om deze informatie te krijgen voordat u de query uitvoert. Laten we ze eens bekijken.

De sys.dm_exec_describe_first_result_set_for_object Functie

De eerste stop is de sys.dm_exec_describe_first_result_set_for_object systeem dynamische beheerfunctie.

Deze functie heeft een @object_id als een parameter en beschrijft de metadata van het eerste resultaat voor de module met die ID.

Het kan worden gebruikt voor opgeslagen procedures en triggers.

Hier is een voorbeeld om te demonstreren. In dit geval gebruik ik de OBJECT_ID() functie om de ID van de opgeslagen procedure terug te geven (waardoor ik de werkelijke ID niet hoef te weten).

SELECT * 
FROM sys.dm_exec_describe_first_result_set_for_object(
    OBJECT_ID('spAlbumsFromArtist'), 
    0
    );  

Resultaat:

+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
| 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              |
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

Als u ver genoeg zijwaarts scrolt, ziet u dat de source_server , source_database , source_schema , source_table , en source_column kolommen zijn NULL . Dit komt omdat we 0 . hebben gebruikt als derde argument.

Het derde argument veranderen in 1 zal informatie teruggeven over de basistabellen waarnaar wordt verwezen in de opgeslagen procedure.

SELECT * 
FROM sys.dm_exec_describe_first_result_set_for_object(
    OBJECT_ID('spAlbumsFromArtist'), 
    1
    );  

Resultaat:

+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
| 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              |
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

Als we nu zijwaarts scrollen, kunnen we zien dat de source_server , source_database , source_schema , source_table , en source_column kolommen bevatten allemaal waarden.

In dit geval doorzoekt de procedure tabellen op een gekoppelde server genaamd "Homer".

U zult ook merken dat er nu vier rijen zijn geretourneerd, in plaats van slechts twee die werden geretourneerd toen we het derde argument instelden op 0 . De twee extra rijen weerspiegelen de onderliggende kolommen die worden opgevraagd binnen de opgeslagen procedure.

Hier is de definitie van de opgeslagen procedure:

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;

Dus hoewel de procedure slechts twee kolommen retourneert, moet hij naar andere kolommen verwijzen om zijn taak te voltooien.

Zie hoe sys.dm_exec_describe_first_result_set_for_object Werkt voor meer voorbeelden.

De sys.dm_exec_describe_first_result_set Bekijk

Deze weergave gebruikt hetzelfde algoritme als de vorige weergave en retourneert dezelfde informatie. Het verschil is dat het een T-SQL-batch analyseert in plaats van de daadwerkelijke opgeslagen procedure.

Dit betekent dat u deze weergave kunt gebruiken om code te analyseren die niet noodzakelijkerwijs in een opgeslagen procedure of trigger zit. U kunt het bijvoorbeeld gebruiken om een ​​weergave of een ad-hocquery te analyseren.

Maar dit artikel gaat over het analyseren van opgeslagen procedures, dus hier is een voorbeeld om precies dat te doen.

SELECT * 
FROM sys.dm_exec_describe_first_result_set(
    'EXEC spAlbumsFromArtist @ArtistId = ''Iron Maiden''',
    null,
    1
    );

Resultaat:

+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+
| 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              |
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+----------------+------------------+---------------+-----------------+--------------+-------------------+

Zie hoe sys.dm_exec_describe_first_result_set Werkt voor meer voorbeelden.

De sp_describe_first_result_set Door het systeem opgeslagen procedure

De sp_describe_first_result_set systeem opgeslagen procedure doet hetzelfde (en gebruikt hetzelfde algoritme). Het accepteert een T-SQL-batch en retourneert de metadata voor de eerste mogelijke resultaatset.

EXEC sp_describe_first_result_set 
    @tsql = N'EXEC spAlbumsFromArtist @ArtistId = ''Iron Maiden''',
    @params = null,
    @browse_information_mode = 1;

Resultaat:

+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------+
| 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                    |
+-------------+------------------+-------------+---------------+------------------+--------------------+--------------+-------------+---------+------------------------------+----------------+----------------------+--------------------+------------------+--------------------------------+---------------------+---------------------------+-------------------------+-----------------------+-------------------+---------------------+----------------------------+-----------------+-------------------+-----------------+----------------+-----------------+----------------------+-------------------------+-----------------+----------------------+------------------------+----------------------------+--------------------------+------------------------+---------------+--------------+--------------------+-------------------------+

Zie hoe sp_describe_first_result_set Werkt voor meer voorbeelden.

OPENROWSET met een tijdelijke tafel

Een andere optie is om een ​​zoekopdracht te maken die de top-nulresultaten van een OPENROWSET . plaatst query in een tijdelijke tabel, gebruik dan een procedure zoals sp_columns om kolominformatie over die tijdelijke tabel te retourneren (die de gegevens uit onze resultatenset bevat).

Dit levert daarom details op, zoals kolomnamen, gegevenstypen, enz.

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');

Resultaat:

+-------------------+---------------+----------------------------------------------------------------------------------------------------------------------------------+---------------+-------------+-------------+-------------+----------+---------+---------+------------+-----------+--------------+-----------------+--------------------+---------------------+--------------------+---------------+----------------+
| 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             |
+-------------------+---------------+----------------------------------------------------------------------------------------------------------------------------------+---------------+-------------+-------------+-------------+----------+---------+---------+------------+-----------+--------------+-----------------+--------------------+---------------------+--------------------+---------------+----------------+

Met deze methode kunt u ook dingen doen als sp_columns swap omwisselen voor sp_help of vergelijkbaar.


  1. Hoe een externe sleutel in SQLite te laten vallen

  2. Alternatief voor Intersect in MySQL

  3. Dubbele rijen in een tabel verwijderen

  4. java.sql.SQLException Parameterindex buiten bereik (1> aantal parameters, dat is 0)