Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sqlexec insert sql server single quote no like 2

Status
Not open for further replies.
Sep 17, 2001
673
US
I am using SQLEXEC to INSERT data into tables on a MS SQL 7 server. I am trying to figure out how to handle when a user enters special characters that will kick back an error. For example:

gcVendorName = "Rob's Stuff"
SQLEXEC(gnHandle, "EXEC sp_Insert_Customer '" +gcVendorName+ "'")

In the above example the gcVendorName has a single quote stored in it. Because of this it throws off sql server and kicks back an error. Is the solution to only allow alphanumeric data? How can I set the fields to only allow alphanumeric data? Is there a better way? Thanks!
 
this code should solve your problem:

MyString = "Rob's Stuff"
gcVendorName = strtran(MyString,"'","''")
SQLEXEC(gnHandle, "EXEC sp_Insert_Customer '" +gcVendorName+ "'")

The only thing added in is the STRTRAN() function to convert a single quote into two single quotes.
 
Have you tried this:

gcVendorName = "Rob's Stuff"
SQLEXEC(gnHandle, "EXEC sp_Insert_Customer ?gcVendorName")
 
to pass through a single quote you need to add another quote to the string
strtran("single ' quote","'","''")
Attitude is Everything
 
Personally, I take a different tack.

Most of the time people are typing single quotes in strings what they really mean to type is an apostrophe. That is the [`] character that is usually to the left of the [1] key on most keyboards.

So in my textbox and editbox base classes when ever they type a single quote I replace it with an apostrophe. (See code below.)

They almost never notice, when they do I explain it to them and the database is happy.


Part of larger case statement in keypress event:

CASE m.nKeyCode = 39 AND m.nShiftAltCtrl = 0
IF !.ReadOnly
** Replace ' keystroke with ` keystroke because
** single quote marks cause problems in SQL Server.
**
NODEFAULT
KEYBOARD "`"
ENDIF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top