Unix Technical Forum

Special Character

This is a discussion on Special Character within the SQL Server forums, part of the Microsoft SQL Server category; --> How can I insert by asp a string containing ' That's the string Asp code: SQL = "insert into ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 05:08 AM
Santo Santis
 
Posts: n/a
Default Special Character

How can I insert by asp a string containing '

That's the string

Asp code:

SQL = "insert into tablename (columnA) values ('" & variable & "')"

The problem is when variable contains a ' (single quote), it stops the
string definition and get an error.

Can I do something?

Thanks



--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 05:08 AM
Craig Kelly
 
Posts: n/a
Default Re: Special Character

"Santo Santis" wrote:
> How can I insert by asp a string containing '
>
> That's the string
>
> Asp code:
>
> SQL = "insert into tablename (columnA) values ('" & variable & "')"
>
> The problem is when variable contains a ' (single quote), it stops the
> string definition and get an error.
>
> Can I do something?
>
> Thanks



In SQL Server, you escape a single quote in a string literal by doubling the
single quote (e.g. 'I can''t dance'). You can do this in your ASP code
with:

SQL = "insert into tablename (columnA) values ('" & Replace(variable, "'",
"''") & "')"

Craig


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 05:08 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Special Character

Santo Santis (santosanto@supereva.it) writes:
> How can I insert by asp a string containing '
>
> That's the string
>
> Asp code:
>
> SQL = "insert into tablename (columnA) values ('" & variable & "')"
>
> The problem is when variable contains a ' (single quote), it stops the
> string definition and get an error.


Be glad that you got a syntax error, and not exposed this on a web
page where an intruder would have used it to inject SQL commands that
you had not intended that web page to execute.

Never build command strings like this, use parameterized commands instead.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:24 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com