T-SQL Smarts: Executing Sp_executesql

In this third in a series on working smarter with T-SQL, let's give the built-in sp_executesql stored proc a go.

In my previous posts, I've described some methods for generating and executing T-SQL dynamically. This time, we'll take a look at one final way of executing T-SQL, which is the built-in system stored procedure sp_executesql.

This stored procedure takes a T-SQL statement (that you've generated) and any associated parameters that you wish pass, and executes it in a new process. It then returns any output (again, if specified) back to the calling process. This can be very handy when writing T-SQL that has variables, or when stringing together sets of dynamic SQL.

Take a look at the this example:

DECLARE @sql nvarchar(1000)
DECLARE @columns varchar(75)
DECLARE @dbname sysname
DECLARE @parms nvarchar(500)
SET @columns = '[name], [compatibility_level], [recovery_model_desc]'
SET @dbname = 'Admin'
SET @parms = '@dbparm sysname'
SET @sql = 'SELECT ' + @columns + ' FROM sys.databases WHERE [name] = @dbparm'
EXECUTE sp_executesql @sql, @parms, @dbparm = @dbname

While in real life you'd never use this complicated of a query to retrieve this information, it illustrates the syntax of the stored procedure easily.

Initially, we must declare a string to be executed (@sql). In this case we also want to be able to list the columns we want (@columns), and we need to be able to specify a database value (@dbname). Finally, we need a variable to hold the definition of the variables we want to use inside of the dynamic SQL command. This has to be defined as an NVARCHAR, but can be any length.

Then, we set the values. @columns and @dbname are obvious. When we set @parms, we're really just defining variables the way we would in any script or stored procedure (without the DECLARE keyword).

Finally, we set the @sql variable, outlining our command. We just use simple string concatenation to combine explicit text with variable values to produce a string that will execute.

Finally, we execute sp_executesql, passing (in order) the command, the parameter definitions, and the value want to set for the “inner” variable; in this case, we're passing the value of @dbname to the dynamic variable @dbparm for use in the command.

We get back this simple result set:

name        compatibility_level  recovery_model_desc
----------- -------------------  -------------------
Admin       100                  FULL
(1 row(s) affected)

Knowing the basic structure of this stored procedure, you can build dynamic commands, and have variables passed into those dynamic commands, in any number of ways.

As always, beware: Run-time compiled commands such as this could be an easy target for SQL injection attacks. Be sure to check your input, and thoroughly test your scripts/stored procedures before executing them in production environments.

Be safe, and have fun!

About the Author

Joshua Jones is co-author of A Developer's Guide to Data Modeling for SQL Server: Covering SQL Server 2005 and 2008 (Addison-Wesley Professional, 2008) and is a principal with Denver-based Consortio Services LLC.


  • Microsoft Uniting OneDrive and SharePoint Admin Portals Next Month

    Microsoft is converging its OneDrive and SharePoint Admin Center management portals, with a consolidated portal expected to arrive for Microsoft 365 subscribers "through February."

  • Phishing Tops Concerns in Microsoft Study of Remote Work

    Potential phishing attacks were a top concern of most IT security professionals when organizations switched to remote-work conditions early last year.

  • How To Configure Windows 10 for Intel Optane Memory

    Intel's Optane memory technology can significantly improve the performance of your Windows 10 system -- provided you enable it correctly. A single mistake can render the system unbootable. Here's how to do it the right way.

  • Microsoft and SAP Enhance Partnership with Teams Integration

    Microsoft and SAP this week described continuing partnership efforts on Microsoft Azure, while also planning a Microsoft Teams integration with SAP's enterprise resource planning product and other solutions.

comments powered by Disqus