How to get the resultset datatypes of a stored procedure ?

  • Hi,

    How to get the resultset datatypes of a stored procedure ?

  • 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[/url]

  • Thanks for the reply Eugene Elutin.

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

    Thanks

  • 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

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • 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[/url]

  • 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.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • 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[/url]

  • 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

  • Deprecated SET FMTONLY statement did the trick

    http://msdn.microsoft.com/en-us/library/ms173839.aspx

  • fregatepllada (8/27/2014)


    Deprecated SET FMTONLY statement did the trick

    http://msdn.microsoft.com/en-us/library/ms173839.aspx%5B/quote%5D

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

    http://www.sqlservercentral.com/Forums/FindPost1573301.aspx

  • patrickmcginnis59 10839 (8/27/2014)


    fregatepllada (8/27/2014)


    Deprecated SET FMTONLY statement did the trick

    http://msdn.microsoft.com/en-us/library/ms173839.aspx%5B/quote%5D

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

    http://www.sqlservercentral.com/Forums/FindPost1573301.aspx

    Further on this, the code in my initial post on that thread will return the structure/metadata of the result set.

    😎

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply