Solved

Handling single quotes in dynamic SQL in Stored Procedures

Posted on 2013-01-16
3
687 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search Text in Views 2 28
SQL Server Agent "Access Denied" Error 3 35
when sa SQL Login becomes locked 6 33
t-sql left join 2 34
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

739 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