Solved

Handling single quotes in dynamic SQL in Stored Procedures

Posted on 2013-01-16
3
678 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach 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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

758 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

22 Experts available now in Live!

Get 1:1 Help Now