Replace Apostrophes for OpenQuery??

Hi All,

I have trigger that sends data from one table to another.
I am using the Openquery statement within the trigger to send the data.

However when I Insert anything with an apostrophe into the table where the trigger resides it errors saying...

"Incorrect syntax near" where the apostrophe is and this puts out the number of Quotes in the Openquery Statement.

I use the Replace function written as  REPLACE(@VARIABLE, '''', '''''') to replace One Apostrophe with 2.
I'm not sure whether I can use this with Openquery or not / or whether the syntax is correct for Replace in conjunction with OpenQuery.

If someone can give me anysight into this and any code on how to do this would be great.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Openquery is a static statement. Use sp_executesql or EXEC().

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Can you post your exact code?
In general if you have characters of ', then you need to double them. The replace you wrote should work fine...

Here is some code example:
declare @t varchar(20)
declare @t2 varchar(20)
create table tmp1 (c1 varchar(30))
set @t='ab'''
set @t2 = @t + 'abc'
declare @ss varchar(200)
set @ss = 'insert into tmp1 values(''' + replace(@t,'''','''''') + ''')'
print @ss
select * from tmp1
select @t
select @t2
select replace(@t2,'''','''''')
select @t2
drop table tmp1
So as you can see the replace is good, but use it just where you need it. In my example I used replace only on the @t variable, not on the entire @ss variable.
Hope this helps..
For replacing apostrophes, you can use QUOTENAME().

Good luck !
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

I compared

[1]. QUOTENAME(@TestString,'''')
[2]. ''''+REPLACE(@TestString,'''','''''')+''''

[1] is 10 % faster than [2].
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
No comment has been added to this question in more than 268 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Accept: ispaleny http:#8193672, xenon_je http:#8193673

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.