Home All Groups Group Topic Archive Search About
Author
7 Jul 2005 2:45 PM
Anders M
Hi!
I have a small string problem in a stored procedure

@sql1='sqlstring to insert to another sqlstring('textstring')'

@sql2='Select type, type2  from' + @sql1 + ' order by ......

exec(@sql2)

How can I insert the ('textstring') into @sql2?

I´ve tried ("textstring") and (textstring) byt that doesn´t work.

Regards
/Anders

Author
7 Jul 2005 2:52 PM
Aaron Bertrand [SQL Server MVP]
You need to escape your quotes.  Think about it:

set @mystring = 'this is a string with 'this is a string' inside of it.'

How does SQL Server know which string delimiters are which?

Try

set @mystring = 'this is a string with ''this is a string'' inside of it.'

That is two apostrophes, ' ', not a double quote, " ... the first apostrohe
acts as an escape character.
Author
7 Jul 2005 2:53 PM
Jens Süßmeyer
DECLARE @Text varchar(100)
SET @Text = 'Test here ''MyText'''
Print @Text

Test here 'MyText'


You have to double yuote that.

HTH, Jens Suessmeyer.

---
http://www.sqlserver2005.de
---


Show quote
"Anders M" <it99***@du.se> wrote in message
news:O3o2AKwgFHA.3656@TK2MSFTNGP09.phx.gbl...
> Hi!
> I have a small string problem in a stored procedure
>
> @sql1='sqlstring to insert to another sqlstring('textstring')'
>
> @sql2='Select type, type2  from' + @sql1 + ' order by ......
>
> exec(@sql2)
>
> How can I insert the ('textstring') into @sql2?
>
> I´ve tried ("textstring") and (textstring) byt that doesn´t work.
>
> Regards
> /Anders
>
>
>
>
>
>
>
Author
7 Jul 2005 2:57 PM
Jeremy Williams
This worked for me (sort of):

------------
DECLARE @sql1 varchar(8000)
DECLARE @sql2 varchar(8000)

Set @sql1='sqlstring to insert to another sqlstring(''textstring'')'
Set @sql2='Select type, type2  from' + @sql1 + ' order by ......'

EXEC(@sql2)
--------------

It does try to execute the statement. Since the example you gave is nonsense
SQL, of course it does not actually work, though. Can you provide the actual
statement, as well as DDL, expected results, and a detailed description of
the error you are receiving? Maybe we can help further, but more information
is needed.

Show quote
"Anders M" <it99***@du.se> wrote in message
news:O3o2AKwgFHA.3656@TK2MSFTNGP09.phx.gbl...
> Hi!
> I have a small string problem in a stored procedure
>
> @sql1='sqlstring to insert to another sqlstring('textstring')'
>
> @sql2='Select type, type2  from' + @sql1 + ' order by ......
>
> exec(@sql2)
>
> How can I insert the ('textstring') into @sql2?
>
> I´ve tried ("textstring") and (textstring) byt that doesn´t work.
>
> Regards
> /Anders
>
>
>
>
>
>
>
Author
7 Jul 2005 4:06 PM
Michael C#
Much danger I see...  opening yourself up for SQL Injection attacks, you
are...

Show quote
"Anders M" <it99***@du.se> wrote in message
news:O3o2AKwgFHA.3656@TK2MSFTNGP09.phx.gbl...
> Hi!
> I have a small string problem in a stored procedure
>
> @sql1='sqlstring to insert to another sqlstring('textstring')'
>
> @sql2='Select type, type2  from' + @sql1 + ' order by ......
>
> exec(@sql2)
>
> How can I insert the ('textstring') into @sql2?
>
> I´ve tried ("textstring") and (textstring) byt that doesn´t work.
>
> Regards
> /Anders
>
>
>
>
>
>
>

AddThis Social Bookmark Button