SQL Server - 参数名称中的无效字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/751719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:43:20  来源:igfitidea点击:

SQL Server - Invalid characters in parameter names

sqlsql-serverparameters

提问by EightyOne Unite

I need to know what are the valid characters to use in a SQL parameter name.

我需要知道在 SQL 参数名称中使用的有效字符是什么。

Given something simple like SELECT * FROM tblTest WHERE testid = @[X], if X contains a hyphen, for instance, the statement will fail. What are the valid characters for parameter names?

给定一些简单的东西,例如SELECT * FROM tblTest WHERE testid = @[X],如果 X 包含一个连字符,则该语句将失败。参数名称的有效字符是什么?

回答by marc_s

Search for "Identifiers" in your SQL Books online, and you should find:

在您的在线 SQL 书籍中搜索“标识符”,您应该会找到:

Rules for Regular Identifiers

常规标识符规则

The rules for the format of regular identifiers depend on the database compatibility level. This level can be set by using sp_dbcmptlevel. When the compatibility level is 90, the following rules apply:

The first character must be one of the following:

  • A letter as defined by the Unicode Standard 3.2. The Unicode definition of letters includes Latin characters from a through z,
    from A through Z, and also letter characters from other languages.
  • The underscore (_), at sign (@), or number sign (#).

Certain symbols at the beginning of an identifier have special meaning in SQL Server. A regular identifier that starts with the at sign always denotes a local variable or parameter and cannot be used as the name of any other type of object. An identifier that starts with a number sign denotes a temporary table or procedure. An identifier that starts with double number signs (##) denotes a global temporary object. Although the number sign or double number sign characters can be used to begin the names of other types of objects, we do not recommend this practice.

Some Transact-SQL functions have names that start with double at signs (@@). To avoid confusion with these functions, you should not use names that start with @@.

Subsequent characters can include the following:

  • Letters as defined in the Unicode Standard 3.2.
  • Decimal numbers from either Basic Latin or other national scripts.
  • The at sign, dollar sign ($), number sign, or underscore.

The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words. Embedded spaces or special characters are not allowed. Supplementary characters are not allowed.

常规标识符的格式规则取决于数据库兼容性级别。可以使用 sp_dbcmptlevel 设置此级别。当兼容级别为 90 时,以下规则适用:

第一个字符必须是以下字符之一:

  • Unicode 标准 3.2 定义的字母。字母的 Unicode 定义包括从 a 到 z、
    从 A 到 Z 的拉丁字符,以及来自其他语言的字母字符。
  • 下划线 (_)、at 符号 (@) 或数字符号 (#)。

标识符开头的某些符号在 SQL Server 中具有特殊含义。以 at 符号开头的常规标识符始终表示局部变量或参数,不能用作任何其他类型对象的名称。以数字符号开头的标识符表示临时表或过程。以双数字符号 (##) 开头的标识符表示全局临时对象。尽管可以使用数字符号或双数字符号字符作为其他类型对象的名称的开头,但我们不推荐这种做法。

某些 Transact-SQL 函数的名称以双符号 (@@) 开头。为避免与这些函数混淆,不应使用以@@ 开头的名称。

后续字符可以包括以下内容:

  • Unicode 标准 3.2 中定义的字母。
  • 来自基本拉丁文或其他国家文字的十进制数字。
  • at 符号、美元符号 ($)、数字符号或下划线。

标识符不能是 Transact-SQL 保留字。SQL Server 保留保留字的大写和小写版本。不允许嵌入空格或特殊字符。不允许使用补充字符。

Search for "delimited identifiers" in your SQL Books online, and you should find:

在您的在线 SQL 书籍中搜索“分隔标识符”,您应该会找到:

The body of the identifier can contain any combination of characters in the current code page, except the delimiting characters themselves. For example, delimited identifiers can contain spaces, any characters valid for regular identifiers, and any one of the following characters.

tilde (~)                hyphen (-)   
exclamation point (!)    left brace ({)   
percent (%)              right brace (})   
caret (^)                apostrophe (')   
ampersand (&)            period (.)   
left parenthesis (()     backslash (\)   
right parenthesis ())    accent grave (`)

标识符的主体可以包含当前代码页中的任意字符组合,但定界字符本身除外。例如,分隔标识符可以包含空格、对常规标识符有效的任何字符以及以下任何一个字符。

tilde (~)                hyphen (-)   
exclamation point (!)    left brace ({)   
percent (%)              right brace (})   
caret (^)                apostrophe (')   
ampersand (&)            period (.)   
left parenthesis (()     backslash (\)   
right parenthesis ())    accent grave (`)

Marc

马克

回答by HLGEM

Search Books Online for identifiers [SQL Server]. It has the rules that parameter names must follow. (this was the SQL Server 2008 search), other versions should be a similar search

在联机丛书中搜索标识符 [SQL Server]。它具有参数名称必须遵循的规则。(这个是SQL Server 2008搜索),其他版本应该类似搜索