Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL won't accept ' character

Author  Topic 

oahu9872
Posting Yak Master

112 Posts

Posted - 2005-11-16 : 12:53:09
i am having a problem with my database forms. if anywhere in the form the ' character is typed in, the sql insert statement fails. i've tried using varchar, nvarchar, and text as the field types with no luck. any ideas how to fix this?

thanks

Vivaldi
Constraint Violating Yak Guru

298 Posts

Posted - 2005-11-16 : 12:57:17
Yep, replace 1 ' with 2 '

________________________________________________

SQL = Serious Quaffing of Liquor
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-11-16 : 14:56:02
How about using stored procedures instead?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-11-16 : 15:37:54
You need a client side function that you use to process the parameters:

strValue = REPLACE(strValue, "'", "''")

and use this on ALL parameters that you pass to SQL Server [using dynamic SQL], otherwise you are leaving yourself wide open to SQL Injection.

Supposing I type into one of your formfields:

FOO'; DELETE * FROM SomeTable; --

how will your system handle that?

If your dynamic SQL is:

"INSERT INTO MyTable (COl1, Col2) VALUES (" + strParameter1 + "," + strParameter2 + ")"

this will translate into

INSERT INTO MyTable (COl1, Col2) VALUES (
FOO'; DELETE * FROM SomeTable; --, BAR)

the first part of which is going to generate a syntax error, and the second part is going to start deleting data.

The example I have given is extremely naive, and won't work in practice - I am not about to describe the "101 steps to hacking a server", but the information is readily available, and you need to make sure your Forms Field Data, and dynamic SQL, is adequately protected, and the least you should do is to REPLACE Single Quotes to "double them up"

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-17 : 00:03:33
Single quote will always give problem when your data have it and you do DMLs, searches,etc
In your presentation layer replace all single quotes to two single quotes by using Replace function as suggested

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-17 : 00:08:09
quote:
Originally posted by X002548

How about using stored procedures instead?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Still you need to handle single quote

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-11-17 : 00:52:03
"Still you need to handle single quote"

Only if you still called them with SQL Command strings In my tests that adds 5% execution time, compared to the AppendParameter type client-side syntax for calling SProcs, so moving to Sprocs also means moving to AppendParameter type calling [in my book!]

Kristen
Go to Top of Page
   

- Advertisement -