Execute Dynamic SQL Commands in SQL Server
Execute Dynamic SQL Commands in SQL Server
tip=1160
Solution
SQL Server offers a few ways of running a dynamically built SQL statement. These ways are:
This first approach is pretty straight forward if you only need to pass parameters into your WHERE clause of your SQL
statement. Let's say we need to find all records from the customers table where City = 'London'. This can be done
easily such as the following example shows.
Latest
2. Using EXEC
SQL Server script to rebuild all indexes for
With this approach you are building the SQL statement on the fly and can pretty much do whatever you need to in all tables and all databases
order to construct the statement. Let's say we want to be able to pass in the column list along with the city.
Locking Down PII Data in SQL Server - Part
For this example we want to get columns CustomerID, ContactName and City where City = 'London'. 2
As you can see from this example handling the @city value is not at straight forward, because you also need to define Restoring a SQL Server database that uses
the extra quotes in order to pass a character value into the query. These extra quotes could also be done within the Change Data Capture
statement, but either way you need to specify the extra single quotes in order for the query to be built correctly and
therefore run. Identifying PII Data to Lock Down in SQL
Server - Part 1
DECLARE @sqlCommand varchar(1000)
DECLARE @columnList varchar(75) Remove HTML tags from strings using the
DECLARE @city varchar(75) SQL Server CLR
SET @columnList = 'CustomerID, ContactName, City'
SET @city = '''London''' Getting started with Code Snippets feature
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM customers WHERE City = ' + @city
of SQL Server Denali
EXEC (@sqlCommand)
SQL Server Stored Procedure Tutorial
With this approach you have the ability to still dynamically build the query, but you are also able to still use Active Comments
parameters as you could in example 1. This saves the need to have to deal with the extra quotes to get the query to
Index Fragmentation Report in SQL Server
build correctly. In addition, with using this approach you can ensure that the data values being passed into the query
are the correct datatypes. 2005 and 2008 (4)
So here are three different ways of writing dynamic queries. In addition to the above, here are some other articles SQL Server snapshot replication fails when
that give you other perspectives on setting up and using dynamic SQL. trying to import data from the distributor
(2)
The Curse and Blessings of Dynamic SQL
Introduction to Dynamic SQL (Part 1) Related
Introduction to Dynamic SQL (Part 2)
Dynamic SQL execution on remote SQL
Next Steps Server using EXEC AT
If at all possible look at avoiding the use of dynamic SQL especially where you start to manipulate the overall Run a Dynamic Query against SQL Server
query string. This could potentially open up other areas of concern such as SQL Injection and performance without Dynamic SQL
issues.
Look into using dynamic SQL in your stored procedures by employing one of the three techniques above instead Recover from a SQL Injection Attack on SQL
having the code generated from your front end application. Server
Sponsor Information
SQL Backup Pro was the smartest kid at school. Head of the class for compression, encryption and centralized
management. Discover why.
SQL Server Issues? Not sure where to turn for answers? Innovative SQL DBA consultants
Valuable SQL Server web casts on Performance Tuning, Development, Administration, Disaster Recovery,
Replication and more...
Community Questions
Latest
Active Questions
Latest
extended property
SQL 2005
Active Questions
replication in sql server (1)
Announcements
Events
Giveaways
Contribute
Events
Giveaways
Giveaways
Win a Kindle with SQL Server eBooks
Contribute