简体   繁体   English

T-SQL 中字符串文字的语法是什么?

[英]What is the syntax of a string literal in T-SQL?

What is the full syntax of a string literal in T-SQL? T-SQL 中字符串文字的完整语法是什么?

I could not find any explicit definition of string syntax in the official documentation, and any blog post I could find only mentioned certain aspects (like the N prefix).我在官方文档中找不到任何明确的字符串语法定义,而且我能找到的任何博客文章都只提到了某些方面(比如N前缀)。 Additionally, any posts on Stack Overflow or other sites that asked how to escape a string had answers that pointed to using parameters instead of answering the question, which doesn't provide me the information I'm looking for.此外,Stack Overflow 或其他网站上询问如何转义字符串的任何帖子都有指向使用参数而不是回答问题的答案,这并没有为我提供我正在寻找的信息。

From what I can tell, a string literal starts with an optional N followed by a wrapper of single quotes (or double quotes, if a setting is set a certain way), single quotes within the string are escaped by doubling the single quote, and backslashes are escaped by doubling the backslash (possibly only if a setting is set a certain way?).据我所知,字符串文字以可选的N开头,后跟单引号(或双引号,如果设置以某种方式设置)的包装,字符串中的单引号通过加倍单引号进行转义,并且反斜杠通过加倍反斜杠进行转义(可能仅当设置以某种方式设置时?)。

Is there an authoritative and/or comprehensive source for this information?该信息是否有权威和/或全面的来源? Even just a list of reserved/special characters would be better than what I was able to find.即使只是保留/特殊字符的列表也会比我能找到的要好。

A String literal is anything enclosed in a single quote ( ' ).字符串文字是用单引号 ( ' ) 括起来的任何内容。 The N prefix isn't "optional" it means that the string following uses the National language character set. N前缀不是“可选的”,它意味着后面的字符串使用国家语言字符集。 A string without the N prefix is a varchar and with is an nvarchar .不带N前缀的字符串是varchar ,带前缀的是nvarchar

The documentation I could not find is here (thanks @GordonLinoff): https://docs.microsoft.com/en-us/sql/t-sql/data-types/constants-transact-sql?view=sql-server-ver15我找不到的文档在这里(感谢@GordonLinoff): https://docs.microsoft.com/en-us/sql/t-sql/data-types/constants-transact-sql?view=sql-server-版本 15

Character string constants are enclosed in single quotation marks and include alphanumeric characters (az, AZ, and 0-9) and special characters, such as exclamation point (,), at sign (@).字符串常量用单引号括起来,包括字母数字字符(az、AZ 和 0-9)和特殊字符,如感叹号 (,)、at 符号 (@)。 and number sign (#).和数字符号 (#)。

If the QUOTED_IDENTIFIER option has been set OFF for a connection, character strings can also be enclosed in double quotation marks, but the Microsoft SQL Server Native Client Provider and ODBC driver automatically use SET QUOTED_IDENTIFIER ON.如果连接的 QUOTED_IDENTIFIER 选项已设置为 OFF,则字符串也可以用双引号引起来,但 Microsoft SQL Server Native Client Provider 和 ODBC 驱动程序会自动使用 SET QUOTED_IDENTIFIER ON。 We recommend using single quotation marks.我们建议使用单引号。

If a character string enclosed in single quotation marks contains an embedded quotation mark, represent the embedded single quotation mark with two single quotation marks.如果单引号内的字符串中包含嵌入的引号,则用两个单引号表示嵌入的单引号。 This is not required in strings embedded in double quotation marks.这在嵌入双引号的字符串中不是必需的。

Unicode strings have a format similar to character strings but are preceded by an N identifier (N stands for National Language in the SQL-92 standard). Unicode 字符串的格式类似于字符串,但前面有一个 N 标识符(N 代表 SQL-92 标准中的国家语言)。 The N prefix must be uppercase. N 前缀必须为大写。

So, by that definition, a Unicode string in the recommended format has the following syntax (as PCRE):因此,根据该定义,推荐格式的 Unicode 字符串具有以下语法(如 PCRE):

N'(?:[^']|'')*'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM