SQL Server 中的单引号和双引号

Ame*_*mee 1 sql sql-server sql-server-2008

我想知道使用引号时发生此错误的确切原因。

INSERT INTO table_check(name) VALUES('hello'hi') -- ERROR
INSERT INTO table_check(name) VALUES('hello''hi') -- RESULT:- hello'hi
INSERT INTO table_check(name) VALUES('hello'''hi') --ERROR
INSERT INTO table_check(name) VALUES('hello''''hi') --RESULT:- hello''hi
INSERT INTO table_check(name) VALUES('hello'''''hi') --ERROR
INSERT INTO table_check(name) VALUES('hello''''''hi') --RESULT:- hello'''hi
Run Code Online (Sandbox Code Playgroud)

Aj.*_*.na 5

单引号通过加倍来转义。因此,只要出现偶数个引号,我们就会得到结果。

要了解单引号的行为,请尝试运行以下代码:

Select '','''','''''','''''''',''''''''''
Run Code Online (Sandbox Code Playgroud)

所以,单引号应该是偶数,否则我们会得到类似的错误:在字符串 ') 之后出现未闭合的引号-- ERROR