Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

sql - 如何在SQL Server中转义单引号?(How do I escape a single quote in SQL Server?)

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

(我正在尝试insert一些文本数据插入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.');

  ask by tim_wonil translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Single quotes are escaped by doubling them up , just as you've shown us in your example.

(单引号可以通过加倍来转义 ,就像您在示例中向我们展示的一样。)

The following SQL illustrates this functionality.

(以下SQL说明了此功能。)

I tested it on SQL Server 2008:

(我在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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...