如何在 SQL Server 中转义单引号?

发布于 2025-01-19 17:15:03 字数 187 浏览 1 评论 0 原文

我正在尝试将一些文本数据插入到 SQL Server 9 中的表中。

该文本包含单引号'

我该如何逃脱呢?

我尝试使用两个单引号,但它引发了一些错误。

例如。 插入 my_table 值('嗨,我的名字''s tim.');

I am trying to insert some text data into a table in SQL Server 9.

The text includes a single quote '.

How do I escape that?

I tried using two single quotes, but it threw me some errors.

eg. insert into my_table values('hi, my name''s tim.');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

凯凯我们等你回来 2025-01-26 17:15:04

怎么样:

insert into my_table values('hi, my name' + char(39) + 's tim.')

How about:

insert into my_table values('hi, my name' + char(39) + 's tim.')
若水微香 2025-01-26 17:15:04

我们中的许多人都知道,逃脱单引号的流行方法是将它们翻倍,就像以下一样。

PRINT 'It''s me, Arul.';

我们将研究一些 逃脱单引号的其他替代方法

1。 Unicode字符

39是单引号的Unicode字符。因此,我们可以像以下一样使用它。

PRINT 'Hi,it'+CHAR(39)+'s Arul.';
PRINT 'Helo,it'+NCHAR(39)+'s Arul.';

2。 Quoted_Identifier

另一个简单且最佳的替代解决方案是使用引用_sidentifier。
当引用_sidentifier被设置为关闭时,可以将字符串用双引号封闭。
在这种情况下,我们不需要逃脱单引号。
因此,在使用单个引号的大量字符串值时,这种方式将非常有帮助。
在使用许多插入/更新脚本的行时,将有很大的帮助。

SET QUOTED_IDENTIFIER OFF;
PRINT "It's Arul."
SET QUOTED_IDENTIFIER ON;

结论

上述方法适用于Azure和前提。

Many of us know that the Popular Method of Escaping Single Quotes is by Doubling them up easily like below.

PRINT 'It''s me, Arul.';

Doubling the Single Quotes Method

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.

PRINT 'Hi,it'+CHAR(39)+'s Arul.';
PRINT 'Helo,it'+NCHAR(39)+'s Arul.';

UNICODE Characters

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.

SET QUOTED_IDENTIFIER OFF;
PRINT "It's Arul."
SET QUOTED_IDENTIFIER ON;

QUOTE_IDENTIFIER

CONCLUSION

The above mentioned methods are applicable to both AZURE and On Premises .

梦里寻她 2025-01-26 17:15:04

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'

梦里泪两行 2025-01-26 17:15:04

引用的加倍应该有效,所以奇怪的是它对你不起作用;但是,另一种方法是在字符串周围使用双引号字符,而不是单引号字符。即,

插入 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.");

可是我不能没有你 2025-01-26 17:15:04

还要注意的另一件事是,它是否真的被存储为经典的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.

开始看清了 2025-01-26 17:15:04

以下语法将只转义一个引号:

SELECT ''''

结果将是一个单引号。可能对于创建动态 SQL 非常有帮助:)。
输入图像描述这里

The following syntax will escape you ONLY ONE quotation mark:

SELECT ''''

The result will be a single quote. Might be very helpful for creating dynamic SQL :).
enter image description here

玩套路吗 2025-01-26 17:15:04

双引号选项对我有帮助

SET QUOTED_IDENTIFIER OFF;
insert into my_table values("hi, my name's tim.");
SET QUOTED_IDENTIFIER ON;

Double quotes option helped me

SET QUOTED_IDENTIFIER OFF;
insert into my_table values("hi, my name's tim.");
SET QUOTED_IDENTIFIER ON;
镜花水月 2025-01-26 17:15:04

只需在要插入的任何内容之前插入一个。这就像SQL Server中的一个逃生字符

示例:

当您有一个字段时,我很好

您可以做:

UPDATE my_table SET row ='I''m fine.';

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:

UPDATE my_table SET row ='I''m fine.';
咽泪装欢 2025-01-26 17:15:04

这应该起作用

DECLARE @singleQuote CHAR 
SET @singleQuote =  CHAR(39)

insert into my_table values('hi, my name'+ @singleQuote +'s tim.')

This should work

DECLARE @singleQuote CHAR 
SET @singleQuote =  CHAR(39)

insert into my_table values('hi, my name'+ @singleQuote +'s tim.')
甜是你 2025-01-26 17:15:04

我遇到了同样的问题,但我的问题不是基于 SQL 代码本身中的静态数据,而是基于数据中的值。

此代码列出了我的数据库中的所有列名称和数据类型:

SELECT DISTINCT QUOTENAME(COLUMN_NAME),DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS

但有些列名称实际上在列名称中嵌入了单引号!,例如...

[MyTable].[LEOS'DATACOLUMN]

要处理这些,我必须同时使用 REPLACE 函数使用建议的 QUOTED_IDENTIFIER 设置。否则,当在动态 SQL 中使用该列时,将会出现语法错误。

SET QUOTED_IDENTIFIER OFF;
    SET @sql = 'SELECT DISTINCT ''' + @TableName + ''',''' + REPLACE(@ColumnName,"'","''") + ...etc
SET QUOTED_IDENTIFIER ON;

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:

SELECT DISTINCT QUOTENAME(COLUMN_NAME),DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS

But some column names actually have a single-quote embedded in the name of the column!, such as ...

[MyTable].[LEOS'DATACOLUMN]

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.

SET QUOTED_IDENTIFIER OFF;
    SET @sql = 'SELECT DISTINCT ''' + @TableName + ''',''' + REPLACE(@ColumnName,"'","''") + ...etc
SET QUOTED_IDENTIFIER ON;
两人的回忆 2025-01-26 17:15:04

只需在值前添加 N

insert into my_table values(N'hi, my name's tim.');

Just Add N before value

insert into my_table values(N'hi, my name's tim.');
素衣风尘叹 2025-01-26 17:15:04

string_escape 可以在SQL Server的较新版本上使用funtion

The STRING_ESCAPE funtion can be used on newer versions of SQL Server

我家小可爱 2025-01-26 17:15:04

这应该有效:使用后斜线并放置双引号

"UPDATE my_table SET row =\"hi, my name's tim.\";

This should work: use a back slash and put a double quote

"UPDATE my_table SET row =\"hi, my name's tim.\";
缱绻入梦 2025-01-26 17:15:03

单引号通过加倍来转义,就像您在示例中向我们展示的那样。以下 SQL 说明了此功能。我在 SQL Server 2008 上测试了它:

DECLARE @my_table TABLE (
    [value] VARCHAR(200)
)
    
INSERT INTO @my_table VALUES ('hi, my name''s tim.')

SELECT * FROM @my_table

结果

value
==================
hi, my name's tim.

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:

DECLARE @my_table TABLE (
    [value] VARCHAR(200)
)
    
INSERT INTO @my_table VALUES ('hi, my name''s tim.')

SELECT * FROM @my_table

Results

value
==================
hi, my name's tim.
℉絮湮 2025-01-26 17:15:03

如果用另一个单引号转义单引号对您不起作用(就像我最近的一个 REPLACE() 查询一样),您可以使用 SET QUOTED_IDENTIFIER OFF code> 在查询之前,然后在查询之后 SET QUOTED_IDENTIFIER ON

例如

SET QUOTED_IDENTIFIER OFF;

UPDATE TABLE SET NAME = REPLACE(NAME, "'S", "S");

SET QUOTED_IDENTIFIER ON;
-- set OFF then ON again

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 use SET QUOTED_IDENTIFIER OFF before your query, then SET QUOTED_IDENTIFIER ON after your query.

For example

SET QUOTED_IDENTIFIER OFF;

UPDATE TABLE SET NAME = REPLACE(NAME, "'S", "S");

SET QUOTED_IDENTIFIER ON;
-- set OFF then ON again
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文