如何在 SQL Server 中转义单引号?
我正在尝试将一些文本数据插入
到 SQL Server 9 中的表中。
该文本包含单引号'
。
我该如何逃脱呢?
我尝试使用两个单引号,但它引发了一些错误。
例如。 插入 my_table 值('嗨,我的名字''s tim.');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
怎么样:
How about:
我们中的许多人都知道,逃脱单引号的流行方法是将它们翻倍,就像以下一样。
我们将研究一些 逃脱单引号的其他替代方法 。
1。 Unicode字符
39是单引号的Unicode字符。因此,我们可以像以下一样使用它。
2。 Quoted_Identifier
另一个简单且最佳的替代解决方案是使用引用_sidentifier。
当引用_sidentifier被设置为关闭时,可以将字符串用双引号封闭。
在这种情况下,我们不需要逃脱单引号。
因此,在使用单个引号的大量字符串值时,这种方式将非常有帮助。
在使用许多插入/更新脚本的行时,将有很大的帮助。
结论
上述方法适用于Azure和前提。
Many of us know that the Popular Method of Escaping Single Quotes is by Doubling them up easily like below.
we are going to look on some other alternate ways of escaping the single quotes.
1. UNICODE Characters
39 is the UNICODE character of Single Quote. So we can use it like below.
2. QUOTED_IDENTIFIER
Another simple and best alternate solution is to use QUOTED_IDENTIFIER.
When QUOTED_IDENTIFIER is set to OFF, the strings can be enclosed in double quotes.
In this scenario, we don’t need to escape single quotes.
So,this way would be very helpful while using lot of string values with single quotes.
It will be very much helpful while using so many lines of INSERT/UPDATE scripts where column values having single quotes.
CONCLUSION
The above mentioned methods are applicable to both AZURE and On Premises .
2种围绕此操作的方法:
对于
'
您可以简单地在字符串中加倍,例如选择'I'M Happpy'
- 将获得:我很高兴
对于您不确定的任何charactor:在SQL Server中,您可以通过
选择unicode(':')
(您保留数字),因此您还可以选择
选择'i'i'+nchar(39)+'m happpy'
2 ways to work around this:
for
'
you can simply double it in the string, e.g.select 'I''m happpy'
-- will get:I'm happy
For any charactor you are not sure of: in sql server you can get any char's unicode by
select unicode(':')
(you keep the number)So this case you can also
select 'I'+nchar(39)+'m happpy'
引用的加倍应该有效,所以奇怪的是它对你不起作用;但是,另一种方法是在字符串周围使用双引号字符,而不是单引号字符。即,
插入 my_table 值(“嗨,我的名字是 tim。”);
The doubling up of the quote should have worked, so it's peculiar that it didn't work for you; however, an alternative is using double quote characters, instead of single ones, around the string. I.e.,
insert into my_table values("hi, my name's tim."
);还要注意的另一件事是,它是否真的被存储为经典的ASCII'(ASCII 27)或Unicode 2019(看起来相似,但不相同)。
这对插入物来说并不重要,但是在选择和更新中,这可能意味着世界。
如果这是Unicode值,则逃脱了“在wery子句中(例如blah ='worker'comp')将返回您所搜索的值,如果'in'worker's comp'''实际上是Unicode值。
如果您的客户端应用程序支持自由关键,以及基于复制和粘贴的输入,则可以在某些行中进行Unicode,而在其他行中可以是ASCII!
一种简单的方法来确认这一点是进行某种开放式查询,以使您要搜索的价值恢复,然后将其复制并粘贴到记事本++或其他一些Unicode支持编辑器中。
不同的外观在ASCII值和Unicode之间,应该对眼睛显而易见,但是如果您朝肛门倾斜,它将在十六进制编辑器中显示为27(ASCII)或92(Unicode)。
Also another thing to be careful of is whether or not it is really stored as a classic ASCII ' (ASCII 27) or Unicode 2019 (which looks similar, but not the same).
This isn't a big deal on inserts, but it can mean the world on selects and updates.
If it's the unicode value then escaping the ' in a WHERE clause (e.g where blah = 'Workers''s Comp') will return like the value you are searching for isn't there if the ' in "Worker's Comp" is actually the unicode value.
If your client application supports free-key, as well as copy and paste based input, it could be Unicode in some rows, and ASCII in others!
A simple way to confirm this is by doing some kind of open ended query that will bring back the value you are searching for, and then copy and paste that into notepad++ or some other unicode supporting editor.
The differing appearance between the ascii value and the unicode one should be obvious to the eyes, but if you lean towards the anal, it will show up as 27 (ascii) or 92 (unicode) in a hex editor.
以下语法将只转义一个引号:
结果将是一个单引号。可能对于创建动态 SQL 非常有帮助:)。
The following syntax will escape you ONLY ONE quotation mark:
The result will be a single quote. Might be very helpful for creating dynamic SQL :).
双引号选项对我有帮助
Double quotes option helped me
只需在要插入的任何内容之前插入一个。这就像SQL Server中的一个逃生字符
示例:
当您有一个字段时,我很好。
您可以做:
Just insert a ' before anything to be inserted. It will be like an escape character in SQL Server
Example:
When you have a field as, I'm fine.
you can do:
这应该起作用
This should work
我遇到了同样的问题,但我的问题不是基于 SQL 代码本身中的静态数据,而是基于数据中的值。
此代码列出了我的数据库中的所有列名称和数据类型:
但有些列名称实际上在列名称中嵌入了单引号!,例如...
要处理这些,我必须同时使用 REPLACE 函数使用建议的 QUOTED_IDENTIFIER 设置。否则,当在动态 SQL 中使用该列时,将会出现语法错误。
I had the same problem, but mine was not based of static data in the SQL code itself, but from values in the data.
This code lists all the columns names and data types in my database:
But some column names actually have a single-quote embedded in the name of the column!, such as ...
To process these, I had to use the REPLACE function along with the suggested QUOTED_IDENTIFIER setting. Otherwise it would be a syntax error, when the column is used in a dynamic SQL.
只需在值前添加 N
Just Add N before value
string_escape 可以在SQL Server的较新版本上使用funtion
The STRING_ESCAPE funtion can be used on newer versions of SQL Server
这应该有效:使用后斜线并放置双引号
This should work: use a back slash and put a double quote
单引号通过加倍来转义,就像您在示例中向我们展示的那样。以下 SQL 说明了此功能。我在 SQL Server 2008 上测试了它:
结果
Single quotes are escaped by doubling them up, just as you've shown us in your example. The following SQL illustrates this functionality. I tested it on SQL Server 2008:
Results
如果用另一个单引号转义单引号对您不起作用(就像我最近的一个
REPLACE()
查询一样),您可以使用SET QUOTED_IDENTIFIER OFF
code> 在查询之前,然后在查询之后SET QUOTED_IDENTIFIER ON
。例如
If escaping your single quote with another single quote isn't working for you (like it didn't for one of my recent
REPLACE()
queries), you can useSET QUOTED_IDENTIFIER OFF
before your query, thenSET QUOTED_IDENTIFIER ON
after your query.For example