簡體   English   中英

sql將值插入特定列

[英]sql insert values into specific column

我正在嘗試將nvarchar值插入表中的特定列。 該值可能具有保留字和單引號字符(如“ not”)。

我所擁有的是:

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

這適用於絕大多數項目,但我收到此錯誤消息:

消息105,第15級,狀態1,第1行
字符串“我們聽不懂,請重新發送或輸入HE”后的引號引起來。
Msg 102,第15級,狀態1,第1行
“我們不理解,請重新發送或輸入HE”附近的語法不正確。

而我實際上錯過了應該放在那的其余文本。 應該是"We didn't understand, please resend, or type HELP and someone will contact you"

任何幫助將不勝感激。

根據注釋,您的輸出字符串為:

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

您將需要轉義字符串周圍的引號,並用四引號將實際上應該是引號的引號引起來。

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

使用方括號可能會幫助:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM