Solved

Apostrophe in WHERE clause in a dynamic SQL statement

Posted on 2004-09-07
2
1,592 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:YLI
  • 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 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now