Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How to get the resultset datatypes of a stored procedure ? Expand / Collapse
Author
Message
Posted Tuesday, February 21, 2012 2:31 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 6:18 AM
Points: 219, Visits: 340
Hi,

How to get the resultset datatypes of a stored procedure ?
Post #1255071
Posted Tuesday, February 21, 2012 3:08 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:23 AM
Points: 2,873, Visits: 5,189
What is the reason you want to do it?
Very strange question... Why would you need to determine the datatypes for recordset returned by stored proc? You should know them from stored proc definition.
Howevere, it's interesting question to answer in T-SQL.
You can find out the columns datatypes of recordset returned by stored proc. To do this:
1. Make sure that Ad Hoc Distributed Queries are allowed on SQL Server:
sp_configure 'Show Advanced Options', 1 
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO



2. User SELECT INTO with OPENROWSET:
select c.* 
from tempdb.sys.columns c
where c.object_id = (select top 1 t.object_id
from tempdb.sys.tables t
where t.name like '#MyTempTable%')

3. Interogate sys.columns and sys.tables to find out column definitions (please note: multiple tables with a name can exist in tempdb, therefore SELECT TOP 1 is used):
select distinct c.* 
from tempdb.sys.columns c
where
join tempdb.sys.tables t on t.object_id = c.object_id
where t.name like '#MyTempTable%'

You can use permanent table to SELCT INTO, in this case your query to get definition will look better, but may create problem for concurrent executions.


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1255085
Posted Tuesday, February 21, 2012 3:28 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 6:18 AM
Points: 219, Visits: 340
Thanks for the reply Eugene Elutin.

Is there any other way without configuring Ad Hoc Distributed Queries option?

Thanks
Post #1255091
Posted Tuesday, February 21, 2012 3:51 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:57 AM
Points: 13,545, Visits: 11,358
deepkt (2/21/2012)
Thanks for the reply Eugene Elutin.

Is there any other way without configuring Ad Hoc Distributed Queries option?

Thanks


Stored procedures actually do not have an explicit metadata contract.
If you do want that, use a table valued function instead.

Stored procedures are not parameterized views




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1255112
Posted Tuesday, February 21, 2012 4:01 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:23 AM
Points: 2,873, Visits: 5,189
deepkt (2/21/2012)
Thanks for the reply Eugene Elutin.

Is there any other way without configuring Ad Hoc Distributed Queries option?

Thanks


No! There is none!
But I still don't understand what you are trying to achieve. Why, in T-SQL, do you need to know the columns datatypes of recordset returned by stored proc?
BTW, if you call stored proc, which returns recordset, from any client app (eg. reporting tools), you will know column datatypes straight away.


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1255118
Posted Tuesday, February 21, 2012 4:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:57 AM
Points: 13,545, Visits: 11,358
Eugene Elutin (2/21/2012)
deepkt (2/21/2012)
Thanks for the reply Eugene Elutin.

Is there any other way without configuring Ad Hoc Distributed Queries option?

Thanks


No! There is none!
But I still don't understand what you are trying to achieve. Why, in T-SQL, do you need to know the columns datatypes of recordset returned by stored proc?
BTW, if you call stored proc, which returns recordset, from any client app (eg. reporting tools), you will know column datatypes straight away.


The problem is that you have to execute the stored procedure first to know the data types. And what if the stored procedure has conditional logic, and the resultset can change?
Views and functions do not have these issues. IMO, a stored procedure is to do something with the data, not just return it.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1255158
Posted Tuesday, February 21, 2012 5:10 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:23 AM
Points: 2,873, Visits: 5,189
Koen Verbeeck (2/21/2012)

The problem is that you have to execute the stored procedure first to know the data types. And what if the stored procedure has conditional logic, and the resultset can change?...


I cannot see the problem with it. Even if stored proc has conditional logic and it may return totally different resultsets, using the method I gave, will be possible to identify the datatypes of the columns in the resultset. For example, these sort of details might be used in creating of dynamic sql...
So, that was the answer to OP question of how to do so.

However, I cannot understand the reason why he would want to do what he asked for!
And that is my question: Why he needs to do this?


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1255164
Posted Tuesday, September 25, 2012 1:26 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:14 PM
Points: 1, Visits: 5
I ran into this issue as well.

Why would you want to know what the data types are going to be coming out of a stored procedure?

Well I am providing services to a larger programming group. They are creating C# objects to hold the data that I am going to pass them however, in my current situation, there is a stored procedure that they are going to need the result set from and as a result would like to know what the field types and max length are going to be .

The stored procedure return comes from a rather complex set of joins , so while I could go and chase down every field and its type/length it would be nicer if I could simply get that on the results end.

So here is the solution I came up with. Part of it was from the above posts, other parts are from other sites. Hope this helps.

First I used OPENROWSET to execute and return the results from the stored procedure into a temporary table.
Of course you're going to want to put in your server info and the stored proceudre that you're trying to evaluate.
SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=SQLMYSERVER\pubweb;Trusted_Connection=yes;',
'EXEC [PrcThatProcedure] ''SomeValuePassed'', 1 ')

Next, you run this script, similar to a previously posted script but with the extra case statement to better label the data type.

select
CASE
WHEN c.system_type_id = 34 THEN 'byte[]'
WHEN c.system_type_id = 35 THEN 'string'
WHEN c.system_type_id = 36 THEN 'System.Guid'
WHEN c.system_type_id = 48 THEN 'byte'
WHEN c.system_type_id = 52 THEN 'short'
WHEN c.system_type_id = 56 THEN 'int'
WHEN c.system_type_id = 58 THEN 'System.DateTime'
WHEN c.system_type_id = 59 THEN 'float'
WHEN c.system_type_id = 60 THEN 'decimal'
WHEN c.system_type_id = 61 THEN 'System.DateTime'
WHEN c.system_type_id = 62 THEN 'double'
WHEN c.system_type_id = 98 THEN 'object'
WHEN c.system_type_id = 99 THEN 'string'
WHEN c.system_type_id = 104 THEN 'bool'
WHEN c.system_type_id = 106 THEN 'decimal'
WHEN c.system_type_id = 108 THEN 'decimal'
WHEN c.system_type_id = 122 THEN 'decimal'
WHEN c.system_type_id = 127 THEN 'long'
WHEN c.system_type_id = 165 THEN 'byte[]'
WHEN c.system_type_id = 167 THEN 'string'
WHEN c.system_type_id = 173 THEN 'byte[]'
WHEN c.system_type_id = 175 THEN 'string'
WHEN c.system_type_id = 189 THEN 'long'
WHEN c.system_type_id = 231 THEN 'string'
WHEN c.system_type_id = 239 THEN 'string'
WHEN c.system_type_id = 241 THEN 'string'
WHEN c.system_type_id = 241 THEN 'string'
END AS [Type]
, c.*
from tempdb.sys.columns c
where c.object_id = (select top 1 t.object_id
from tempdb.sys.tables t
where t.name like '#MyTempTable%')

Hope this helps.

~Wade Cantley
Post #1364257
Posted Wednesday, August 27, 2014 8:44 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 6, 2014 2:22 PM
Points: 35, Visits: 173
Deprecated SET FMTONLY statement did the trick
http://msdn.microsoft.com/en-us/library/ms173839.aspx
Post #1607867
Posted Wednesday, August 27, 2014 9:22 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 8:07 AM
Points: 377, Visits: 2,613
fregatepllada (8/27/2014)
Deprecated SET FMTONLY statement did the trick
http://msdn.microsoft.com/en-us/library/ms173839.aspx


also check sp_describe_first_result_set, thanks to Eirikur Eiriksson for heads up on that one!

http://www.sqlservercentral.com/Forums/FindPost1573301.aspx
Post #1607891
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse