Enables capturing secondary SQL statement outputs, such as recordsets, print messages and more.
  • Stored Procedure
  • SQL Server Compatibility: 2008, 2012, 2014, 2016, 2017


EXEC SQLHTTP.net.ExecSQL @SQL [, @TableIndex][, @ConnectionString][, @DisplayTableDefinition][, @Messages xml OUTPUT]


Name Type Description
@SQL nvarchar(MAX) SQL Statements to be executed.
@TableIndex int Optional. The ordinal position of the table to retrieve. Default: 1
@ConnectionString nvarchar(4000) Optional. A connection string to a SQL Server instance, if not the current SQL Server instance, or if alternate credentials are needed.
@DisplayTableDefinition bit Optional. A boolean indicator. If 1 (true), a table definition of the retrieved recordset will be outputted as well. Default 0 (false)
@Messages xml Optional. Output Parameter. An XML containing all the messages and/or errors collected during the procedure’s execution.


This stored procedure has many similarities to SQL Server’s EXEC statement and sp_executesql stored procedure, but also has the following enhancements:

  • The @SQL parameter is of type nvarchar(MAX) which allows for a larger script to be executed.
  • Ability to retrieve any of the recordsets generated by the SQL script, not only the first one.
  • A Connection String to a remote server without the use of a linked server.
  • Display of the table structure of the outputted recordset for easy usage in an INSERT… EXEC statement.
  • An XML output of all messages including error messages.

Sample Usage:

[SingleCellTable2] int NULL)

INSERT INTO @Table([SingleCellTable2])
@SQL = ‘
SELECT 1 AS SingleCellTable1
SELECT 2 AS SingleCellTable2
PRINT ”This text was generated using the PRINT statement”
SELECT 1/0’,
@TableIndex = 2

See Also: