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

发布于 2024-08-08 15:12:38 字数 187 浏览 3 评论 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

羞稚 2024-08-15 15:12:38

单引号通过加倍来转义,就像您在示例中向我们展示的那样。以下 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.
羞稚 2024-08-15 15:12:38

如果用另一个单引号转义单引号对您不起作用(就像我最近的一个 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
小草泠泠 2024-08-15 15:12:38

我们很多人都知道,转义单引号的流行方法是通过将它们加倍,如下所示。

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

双倍单倍引号方法

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

1. UNICODE字符

39是单引号的UNICODE字符。所以我们可以像下面这样使用它。

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

UNICODE 字符

2. QUOTED_IDENTIFIER

另一个简单且最佳的替代解决方案是使用 QUOTED_IDENTIFIER。
当 QUOTED_IDENTIFIER 设置为 OFF 时,字符串可以用双引号引起来。
在这种情况下,我们不需要转义单引号。
因此,在使用大量带单引号的字符串值时,这种方法非常有用。
在使用如此多行 INSERT/UPDATE 脚本(其中列值带有单引号)时,这将非常有帮助。

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

QUOTE_IDENTIFIER

结论

上述方法适用于 AZURE 和 On Premises。

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 .

新人笑 2024-08-15 15:12:38

怎么样:

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.')
那些过往 2024-08-15 15:12:38

解决此问题的两种方法:


对于',您只需在字符串中将其加倍即可,例如
select 'I'm happy' -- 将得到:I'm happy


对于任何你不确定的字符:在 sql server 中你可以通过以下方式获取任何字符的 unicode select unicode(':') (你保留号码)

所以这种情况你也可以select 'I'+nchar(39)+'m happy'

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'

决绝 2024-08-15 15:12:38

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

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

甩你一脸翔 2024-08-15 15:12:38

另一件需要注意的事情是它是否真的存储为经典的 ASCII ' (ASCII 27) 或 Unicode 2019(看起来相似,但不一样)。

这对于插入来说并不是什么大问题,但它可能意味着选择和更新的世界。
如果它是 unicode 值,则转义 WHERE 子句中的 '(例如,where blah = 'Workers''s Comp')将返回,就像您正在搜索的值不存在一样,如果“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.

橪书 2024-08-15 15:12:38

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

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

∞琼窗梦回ˉ 2024-08-15 15:12:38

双引号选项帮助了我

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;
情未る 2024-08-15 15:12:38

只需在要插入的内容之前插入一个 ' 即可。它就像 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.';
幸福不弃 2024-08-15 15:12:38

这应该有效

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.')
且行且努力 2024-08-15 15:12:38

我遇到了同样的问题,但我的问题不是基于 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;
清风夜微凉 2024-08-15 15:12:38

只需在值前添加 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.');
ζ澈沫 2024-08-15 15:12:38

STRING_ESCAPE 函数可用于较新版本的 SQL Server

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

耀眼的星火 2024-08-15 15:12:38

这应该有效:使用反斜杠并加上双引号

"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.\";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文