?
Solved

Apostrophe in WHERE clause in a dynamic SQL statement

Posted on 2004-09-07
2
Medium Priority
?
1,694 Views
Last Modified: 2011-10-03
Could anyone help me with the following apostrophe problem with MS SQL Server 2000? I searched this site but couldn't find a good solution. Some answers are about the INSERT or UPDATE part of the SQL statement, NOT the WHERE part as what I need.

NOTE: The following is used in a SP on the server, NOT via a Front End.

I have a dynamic SQL statement to update data in tables, but it will fail whenever the field 'Customer_Name' contains an apostrophe in the WHERE part.

Here are some details:

-- some code omitted, including variables....

-- Update 'Sales_Total' in Table '@vchDataSource (this table name is dynamic, such as 'tblSales_Summary').
-- On this particular occasion, Customer_Name (@vchCustomerName ) is unique but may contain apostrophe(s), which will casue a problem.

SELECT @vchSQL ='UPDATE ' + @vchDataSource +
                              '  SET Sales_Total= ' + CONVERT(VARCHAR, @mnySales_Total_ByCustomer) +
                              ',  Last_Update = '''  + CONVERT(VARCHAR, @dtmCurrentDate)  +
                     '''  WHERE Customer_Name = ''' +   @vchCustomerName + ''''

EXEC (@vchSQL)



0
Comment
Question by:Yongshu Li
[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
  • 2
2 Comments
 
LVL 18

Expert Comment

by:SjoerdVerweij
ID: 12000893
SELECT @vchSQL ='UPDATE ' + @vchDataSource +
                              '  SET Sales_Total= ' + CONVERT(VARCHAR, @mnySales_Total_ByCustomer) +
                              ',  Last_Update = '''  + CONVERT(VARCHAR, @dtmCurrentDate)  +
                    '''  WHERE Customer_Name = ''' +   replace(@vchCustomerName,  '''', '''''') + ''''

Note that the second parameter to Replace is 4 single quotes; the third 6 single quotes.
0
 
LVL 18

Accepted Solution

by:
SjoerdVerweij earned 2000 total points
ID: 12000914
Actually,

SELECT @vchSQL ='UPDATE ' + @vchDataSource +
                              '  SET Sales_Total= ' + CONVERT(VARCHAR, @mnySales_Total_ByCustomer) +
                              ',  Last_Update = '''  + CONVERT(VARCHAR, @dtmCurrentDate)  +
                    '  WHERE Customer_Name = ''' +   replace(@vchCustomerName,  '''', '''''') + ''''
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

800 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