Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get the resultset datatypes of a stored procedure ?


How to get the resultset datatypes of a stored procedure ?

Author
Message
deepkt
deepkt
SSC Veteran
SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)

Group: General Forum Members
Points: 235 Visits: 358
Hi,

How to get the resultset datatypes of a stored procedure ?
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3036 Visits: 5478
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
deepkt
deepkt
SSC Veteran
SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)

Group: General Forum Members
Points: 235 Visits: 358
Thanks for the reply Eugene Elutin.

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

Thanks
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16332 Visits: 13197
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 SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3036 Visits: 5478
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16332 Visits: 13197
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 SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3036 Visits: 5478
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
chris 6112
chris 6112
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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
fregatepllada
fregatepllada
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 322
Deprecated SET FMTONLY statement did the trick
http://msdn.microsoft.com/en-us/library/ms173839.aspx
patrickmcginnis59 10839
patrickmcginnis59 10839
SSC Eights!
SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)SSC Eights! (859 reputation)

Group: General Forum Members
Points: 859 Visits: 5074
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

to properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search