Solved

Handling single quotes in dynamic SQL in Stored Procedures

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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

803 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