Solved

Handling single quotes in dynamic SQL in Stored Procedures

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

679 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