In Previous versions of SQL Server, to get the output format of a query or stored procedure, we should use SET FMTONLY ON/OFF
Let us see how SET FMTONLY works
USE AdventureWorks2008R2; GO SET FMTONLY ON; GO EXEC uspGetEmployeeManagers @BusinessEntityID=16 GO SET FMTONLY OFF; GO
Executing above query, returns the list of columns returned as output from the above SP
SQL Server “Denali” provides us 2 new System Stored procedures and 2 new DM Functions to returns this as query output
1. sp_describe_first_result_set
This system stored procedures returns the list of output columns from the first result set of passed stored procedure or query. Please note, If the SP returns multiple results set, this will provide us the output columns for first result set only
1: EXEC sp_describe_first_result_set @tsql= N'EXEC uspGetEmployeeManagers @BusinessEntityID=16'
Output:
2. sys.dm_exec_describe_first_result_set(@tsql nvarchar(MAX), @params nvarchar(max), @include_browse_information bit)
This DM Function works the same way as sp_describe_first_result_set, but you can use this to join with other queries easily
1: SELECT * FROM sys.dm_exec_describe_first_result_set(N'EXEC uspGetEmployeeManagers @BusinessEntityID=16', null, 0)
You can even use this to identify the output columns and its information for adhoc queries as well
1: SELECT * FROM sys.dm_exec_describe_first_result_set(N'SELECT 1 + 1 [intA] UNION SELECT 2 [intB]', null, 0)
1: SELECT * FROM sys.dm_exec_describe_first_result_set(N'SELECT DB_ID()', null, 0)
3. sys.dm_exec_describe_first_result_set_for_object(@object_id int, @include_browse_information bit)
This DM function accepts an object id as input parameter instead of T-SQL and returns the list of output columns from the first result set. The input object id can be Stored Procedure or Trigger’ object id
1: SELECT * FROM sys.dm_exec_describe_first_result_set_for_object(OBJECT_ID(N'uspGetEmployeeManagers'), 0)
How to get first result set’ output columns of all stored procedures with name contains ‘emp’
1: SELECT SPs.name, Results.column_ordinal, Results.name, Results.system_type_id, Results.system_type_name,
2: Results.max_length, Results.precision, Results.scale, Results.collation_name, Results.user_type_database, Results.user_type_schema
3: FROM sys.procedures AS SPs
4: CROSS APPLY sys.dm_exec_describe_first_result_set_for_object (SPs.object_id, 0) AS Results;
5: WHERE SPs.name LIKE '%Emp%';
4. sp_describe_undeclared_parameters (@TSQL NVarchar(MAX), @Params NVarchar(Max))
This System stored procedures returns the result set of meta data about undeclared parameters specified the TSQL query passed as parameter
ex.
1: EXEC sp_describe_undeclared_parameters @tsql = N'SELECT * FROM sys.databases WHERE database_ID > @DBID'
The output of the above call returns us what is the possible data type for @DBID
I hope you will find this useful to get the meta data of stored procedures developed by others to find the output without executing it.
Filed under: SQL Server "Denali" Tagged: Meta Data Discovery, SQL Server, SQL Server "Denali" New Features, SQL Server Denali
