Solved

Handling single quotes in dynamic SQL in Stored Procedures

Posted on 2013-01-16
3
680 Views
Last Modified: 2013-01-16
How do I format usage of single quotes when building a dynamic SQL statement in a stored procedure?  The dynamic SQL statement itself will contain single quotes.

Here’s the SQL that I want the SP to run:

UPDATE [WORKSTATION\SQLEXPRESS].[DATABASE].[dbo].[Transactions]
                SET [UploadStatus] = 'NOUPLOAD'
                WHERE ID = ''

This runs fine when I run it as shown, as a normal static update command.


Here's the dynamic SQL statement I'm working on (I am passing the @wsName parameter from a step in the Job that calls the SP):
 
SET @SQL1 = 'UPDATE [' + @wsName + '].DATABASE.dbo.TRANSACTIONS
                  SET [UPLOADSTATUS] = ' + 'NOUPLOAD' +
                  'WHERE ID = ' + ''''


However, I'm running into problems because the dynamic SQL needs to contain single quotes.

How do I differentiate a single quote as a delimiter of the dynamic SQL statement I'm building, and a single quote as one that I actually want in the resulting dynamic SQL?

Thank you.
0
Comment
Question by:JohnZeek
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 38784691
the rule is simple, replace your single quote with two single qoutes within the dynamic sql
0
 
LVL 3

Assisted Solution

by:LordKnightshade
LordKnightshade earned 250 total points
ID: 38784861
Anee's rule is correct, and you can eliminate some of the additional concatenation in your string.  Also remember to trim down any variables to remove unwanted whitespace and to cast non character variables so they don't error.

Converted the above into something like this:
DECLARE @SQL1 NVARCHAR(500), @wsName NVARCHAR(50), @SomeID INT
SELECT @wsName = 'SQLServer1', @SomeID = 123456
SET @SQL1 = 'UPDATE [' + RTRIM(@wsName) + '].DATABASE.dbo.TRANSACTIONS
	SET [UPLOADSTATUS] = ''NOUPLOAD''
	WHERE ID = ' +CAST(@SomeID AS NCHAR)+''
                  
SELECT @SQL1

Open in new window

0
 

Author Closing Comment

by:JohnZeek
ID: 38785090
I eventually stumbled onto the same solution anee suggested.  Lord's suggestion is also valuable and I'll try to utilize those suggestions as well.  Thanks to both.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now