Question

This is a meta-question. How does one, using the INFORMATION_SCHEMA provided with each database, discover the return value(s) of a stored procedure? With functions, the return value is explicitly declared and shows up in the INFORMATION_SCHEMA under ROUTINES.

However, stored procedures seem to be a bizarre grey area (as always is the distinction between SP's and functions). They seem to officially have no 'return value', but yet at the end you can run a SELECT statement such as:

SELECT RowID = @RowID;

For example; and in C# you read the return value from the column 'RowID'. This makes it clear that stored procedures actually are capable of returning values, albeit not within the context of T-SQL code like functions do.

How does one discover these values and their types without having to parse the definition itself?

EDIT

If you are searching for this, the return values of stored procedures are integers implicitly, but also can return data called result sets.

Was it helpful?

Solution

Stored procedures return integers.

If you want the result sets they return you can ask the SQL Server to parse the stored procedure to give you this information.

SQL Server 2012 introduces sys.dm_exec_describe_first_result_set_for_object which replaces SET FMTONLY.

CREATE PROC TestProc2
AS
SELECT object_id, name FROM sys.objects ;
SELECT name, schema_id, create_date FROM sys.objects ;
GO

SELECT * FROM sys.dm_exec_describe_first_result_set_for_object(OBJECT_ID('TestProc2'), 0) ;
SELECT * FROM sys.dm_exec_describe_first_result_set_for_object(OBJECT_ID('TestProc2'), 1) ;
GO

You can also investigate the parameters of a stored procedure, to see if any of them are marked OUT. INFORMATION_SCHEMA.PARAMETERS gives you each parameter's mode.

Looking at all three in combination should give you a lot of what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top