0

I'm trying to insert a nvarchar value into a specific column in a table. The value can possibly have reserved words and single quote chars(as in don't).

what I have is:

set @myString= REPLACE(@myString, '''', '''''');
set @ExecStatement = 'INSERT INTO #TempTable('+@ColumnName+') VALUES('''+@myString+''')';
exec (@ExecStatement)

This works for the vast majority of items but I get this error message:

Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string 'We didn't understand, please resend, or type HE'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'We didn't understand, please resend, or type HE'.

and I'm actually missing the remainder of the text that's supposed to be there. It's supposed to be "We didn't understand, please resend, or type HELP and someone will contact you".

Any help would be greatly appreciated.

6
  • You should escape single quotes out of your input. Commented Apr 18, 2014 at 17:35
  • Since you are properly escaping the string, it should work. Is there a trigger on the table that is using dynamic SQL? Commented Apr 18, 2014 at 17:44
  • Can you do a SELECT @ExecStatement before the exec (@ExecStatement) and show us what the result is? Commented Apr 18, 2014 at 17:49
  • Perhaps the problem is the length of the strings. Can you show the declare statement. Commented Apr 18, 2014 at 18:02
  • @Tom here's the select results: INSERT INTO #TempTable(Col1) VALUES('We didn''t understand, please resend, or type HELP and someone will contact you.') Commented Apr 18, 2014 at 18:09

2 Answers 2

1

Based on the comment that your output string is:

INSERT INTO #TempTable(Col1) VALUES('We didn''t understand, please resend, or type HELP and someone will contact you.')

You will need to escape the quotes surrounding the string, and quadruple quote the ones that should actually be quotes... Something like

INSERT INTO #TempTable(Col1) VALUES(''We didn''''t understand, please resend, or type HELP and someone will contact you.'')
Sign up to request clarification or add additional context in comments.

3 Comments

meaning set @myString= REPLACE(@myString, '''', ''''''); would actually be set @myString= REPLACE(@myString, '''', '''''''''');?
Keep the REPLACE on @myString the same, but then do the same thing on @ExecStatement
So doing the REPLACE ON THE @ExecStatement did some funny things, so I just did set @myString= REPLACE(@myString, '''', ''''''''''); and that resolved the issue.
0

Using square brackets may help:

set @myString= REPLACE(@myString, '''', '''''');
set @ExecStatement = 'INSERT INTO #TempTable(['+@ColumnName+']) VALUES('''+@myString+''')';
exec (@ExecStatement)

3 Comments

the column name isn't the problem I'm having, it's the @myString that's giving me issues.
@shadonar . . . Somehow, you may know that. However, we don't. It would be helpful if you edited the question to include the value of @ExecStatement after the variable substitution. Or at least the value of @ColumnName.
Maybe try VALUES('N''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.