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
 General SQL Server Forums
 New to SQL Server Programming
 Quotes in String Parameter

Author  Topic 

barnabeck
Posting Yak Master

236 Posts

Posted - 2012-05-09 : 06:51:56
I wrote a function that converts our queries after databases had changed their names and structure; the query is passed as a String parameter of type varchar(max), but for some reason the function only works if the string does not contain any quote character. So I first replace them in a text editor with any Placeholder which is rather inconvenient.

I managed to include the back conversions (I do the same thing on carriage returns) in the function itself

SET @QueryOut=REPLACE(@QueryOut,'&return;', CHAR(10))
SET @QueryOut=REPLACE(@QueryOut,'"e;', CHAR(39))

My question is: If I can do the back replacement, and @QueryOut containing quotes can be perfectly displayed, why is it impossible to assign:

set @QueryIn='select * from dbo.test where var='test''

How can I achieve this?

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-09 : 07:03:36
[code]
You need to place string value in Double quotes.''test''

set @QueryIn='select * from dbo.test where var=''test'''[/code]
Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2012-05-09 : 07:12:55
Ok. But this requires a manual replacement, which I am trying to avoid... The string parameter should be passed whithout any prior manipulation, any further replacement necessary should be done within the function... which is probably not possible!

Thank you anyway!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-10 : 08:52:42
You should use a parameter and pass value to it. Your method will lead to SQL Injection

Madhivanan

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

- Advertisement -