CiucaS CiucaS - 1 year ago 116
SQL Question

get returned fields datatypes from a stored procedure

I've got a small problem I ran into. I want o create a form that ask for a procedure name and returns the params of the stored procedure and the columns that the procedure returns.

For example

@Param1 int,
@Param2 varchar(1)
WHERE Param1 = @Param1 and Param2 = @Param2

I know I can get the params of a procedure from SQL Server using this query

'Parameter_name' = name,
'Type' = type_name(user_type_id),
'Length' = max_length,
'Prec' = case when type_name(system_type_id) = 'uniqueidentifier'
then precision
else OdbcPrec(system_type_id, max_length, precision) end,
'Scale' = OdbcScale(system_type_id, scale),
'Param_order' = parameter_id,
'Collation' = convert(sysname,
case when system_type_id in (35, 99, 167, 175, 231, 239)
then ServerProperty('collation') end)

from sys.parameters where object_id = object_id('[dbo].[abc]')

and from what I've read on the web, there is no easy way to get the returned datatypes and their names from a stored procedure as the result may be different based on the input params.

I would love to know if there is a way to get the returned datatypes and their names from SQL, but I wouldn't mind if anyone can tell me, or guide me to a place where I can find a solution to get the returned datatypes and their names from Delphi. Something like when you click on a
and click on AddAllFields option.

Hope I made myself understood.

Thank you

Answer Source

You can use sys.dm_exec_describe_first_result_set.

See the example B "Returning information about a procedure":

USE AdventureWorks2012;

CREATE PROC Production.TestProc
SELECT Name, ProductID, Color FROM Production.Product ;
SELECT Name, SafetyStockLevel, SellStartDate FROM Production.Product ;

SELECT * FROM sys.dm_exec_describe_first_result_set
('Production.TestProc', NULL, 0) ;

It uses the same algorithm as sp_describe_first_result_set, so you can find some remarks about this at:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download