Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8128329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:50:23+00:00 2026-06-06T07:50:23+00:00

I am looking at whether it is possible to execute arbitrary SQL commands (dynamic

  • 0

I am looking at whether it is possible to execute arbitrary SQL commands (dynamic SQL statements or stored procedures) and return the result as XML, in an Azure SQL Database.

I know it can be done with regular, on premises SQL Server database – in our case we use a CLR function. Alternatives ‘regular’ solutions include using OPENROWSET or OPENQUERY, neither of which is available for Azure.

A new EXECUTE … AS FOR XML option is available as of SQL Server 2012, but when I try it I get an error – I am unable to locate correct examples of its use.

exec ('select ''A'', 2, ''d''')
with result sets (as for xml)

returns

Msg 11537, Level 16, State 1, Line 1
EXECUTE statement failed because its WITH RESULT SETS clause specified 1 column(s) for result set number 1, but the statement sent 3 column(s) at run time.

To be explicitly clear; I can’t control the command being passed – it is most likely a stored procedure, and it most likely returns a single ‘regular’ (i.e. non-xml) resultset. The use case for this is in a set of SQL tests, not the actual production code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:50:25+00:00Added an answer on June 6, 2026 at 7:50 am

    I tracked down an authoritative answer from Steve Howard, Sr. Program Manager with Microsoft on the Customer Advisory Team:

    AS FOR XML will not convert non-XML tabular results from
    the executed statement or stored procedure into XML.
    AS FOR XML specifies that the XML results from the statement or stored
    procedure called by the EXECUTE statement will be converted into the
    format as if they were produced by a SELECT … FOR XML … statement. All
    formatting from the type directives in the original statement will be
    removed, and the results returned will be as if no type directive was
    specified.

    But he also provided me with this alternative:

    /*
           Demo of how to display results from a stored procedure in XML
           Author: Steve Howard
           Date: June 21, 2012
           Intended as a demo only. Adapt to your purposes
    
    */
    
    -- for this demo, declare a variable @tsql_batch to hold the batch to be executed
    -- note that this could become a parameter in a stored procedure if this is how this is used
    
    declare @tsql_batch  nvarchar(4000)
    
    -- set the variable for the demo only
    -- you will need to set this in your testing
    
    set @tsql_batch = N'exec forDemo'
    
    -- declare a table to hold the results of sp_describe_first_result_set
    -- note that this can also be used for multiple result sets. See the documentation
    -- at: http://technet.microsoft.com/en-us/library/ff878602(v=sql.110).aspx
    declare @resultDescription table
    (
           is_hidden                                       bit                        null
    ,      column_ordinal                                  int                        not null primary key
    ,      name                                            sysname                    not null
    ,      is_nullable                                     bit                        null
    ,      system_type_id                                  int                        null
    ,      system_type_name                         nvarchar(256) null
    ,      max_length                                      smallint             null
    ,      precision                                       tinyint                    null
    ,      scale                                           tinyint                    null
    ,      collation_name                                  sysname                    null
    ,      user_type_id                             int                        null
    ,      user_type_database                       sysname                    null
    ,      user_type_schema                         sysname                    null
    ,      user_type_name                                  sysname                    null
    ,      assembly_qualified_type_name      nvarchar(4000)       null
    ,      xml_collection_id                        int                        null
    ,      xml_collection_database                  sysname                    null
    ,      xml_collection_schema                    sysname                    null
    ,      xml_collection_name                      sysname                    null
    ,      is_xml_document                                 bit                        null
    ,      is_case_sensitive                        bit                        null
    ,      is_fixed_length_clr_type          bit                        null
    ,      source_server                            sysname                    null
    ,      source_database                                 sysname                    null
    ,      source_schema                            sysname                    null
    ,      source_table                             sysname                    null
    ,      source_column                            sysname                    null
    ,      is_identity_column                       bit                        null
    ,      is_part_of_unique_key                    bit                        null
    ,      is_updateable                            bit                        null
    ,      is_computed_column                       bit                        null
    ,      is_sparse_column_set              bit                        null
    ,      ordinal_in_order_by_list          smallint             null
    ,      order_by_list_length              smallint             null
    ,      order_by_is_descending                   smallint             null
    ,      tds_type_id                                     int                        null
    ,      tds_length                                      int                        null
    ,      tcs_collation_id                         int                        null
    ,      tds_collation_sort_id                    tinyint                    null
    )
    
    -- populate the table variable
    insert into @resultDescription
           exec sp_describe_first_result_set @tsql_batch
    
    -- declare the cursor to create the "create table statement:
    declare crs cursor for SELECT '[' + name + '] ' + system_type_name + ' ' + case is_nullable when 0 then 'not null ' else 'null ' end + '
    ' FROM @resultDescription order by column_ordinal asc
    
    -- variables to hold the statement to be executes as well as the current value from the cursor
    declare @exec NVARCHAR(4000)
    declare @curVal      nvarchar(1000)
    open crs
    fetch next from crs into @curval
    -- begin building the dynamic SQL statement to be executed
    set @exec = 'DECLARE @temp TABLE 
    (
    ' + @curval
    
    fetch next from crs into @curval
    
    while @@FETCH_STATUS = 0
    begin
           set @exec = @exec + ', ' + @curVal
           fetch next from crs into @curVal
    end
    close crs
    deallocate crs
    set @exec = @exec + '
    )
    
    
    INSERT INTO @temp
           exec sp_executesql N''' + @tsql_batch + '''
    
    SELECT * FROM @temp FOR XML AUTO
    
    ' 
    
    
    -- get the results
    exec (@exec)
    
    
    -- of you want to just see the statement that was executed:
    
    print @exec
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for advice and whether what I want to do is possible. I've
I'm looking for some advice on whether or not I should use a separate
I'm looking for an algorithm to test whether or not a polygon is 'strictly'
I'm looking for a way to use your location to determine whether you need
I'm looking at PHP date() documentation and thought that 'I' would tell me whether
I am looking for a library or database that can provide guesses about whether
I'm looking into the whole quoted-printable methodology and I'm wondering whether it is worth
I'm looking for a visual regression testing tool for CSS refactoring and see whether
I have been looking into HTML5 manifest but I am unclear as to whether
While looking into parallel programming, and subsequently evaluation strategies, the question whether thunks are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.