SQL Server - 参数名称中的无效字符
我需要知道在 SQL 参数名称中使用哪些有效字符.
I need to know what are the valid characters to use in a SQL parameter name.
给定类似 SELECT * FROM tblTest WHERE testid = @[X]
的简单代码,例如,如果 X 包含连字符,则语句将失败.参数名称的有效字符是什么?
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?
推荐答案
在您的 SQL 在线书籍中搜索标识符",您应该找到:
Search for "Identifiers" in your SQL Books online, and you should find:
常规标识符规则
常规标识符的格式规则取决于数据库兼容级别.可以使用 sp_dbcmptlevel 设置此级别.当兼容级别为 90 时,以下规则适用:
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:
- Unicode 标准 3.2 定义的字母.字母的 Unicode 定义包括从 a 到 z 的拉丁字符,
从 A 到 Z,以及来自其他语言的字母字符. - 下划线 (_)、at 符号 (@) 或数字符号 (#).
标识符开头的某些符号具有特殊性SQL Server 中的意思.以 at 开头的常规标识符符号始终表示局部变量或参数,不能使用作为任何其他类型对象的名称.开头的标识符带数字符号表示临时表或过程.一个以双数字符号 (##) 开头的标识符表示全局临时对象.虽然数字符号或双数字符号字符可用于开始其他类型的对象的名称,我们不推荐这种做法.
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.
某些 Transact-SQL 函数的名称以双符号开头(@@).为避免与这些功能混淆,您不应使用以@@ 开头的名称.
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:
- Unicode 标准 3.2 中定义的字母.
- 来自基本拉丁文或其他国家文字的十进制数字.
- at 符号、美元符号 ($)、数字符号或下划线.
标识符不能是 Transact-SQL 保留字.数据库服务器保留保留字的大写和小写版本.不允许嵌入空格或特殊字符.补充不允许使用字符.
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.
在您的在线 SQL 书籍中搜索分隔标识符",您应该找到:
Search for "delimited identifiers" in your SQL Books online, and you should find:
标识符的主体可以包含中字符的任意组合当前代码页,除了分隔字符本身.为了例如,分隔标识符可以包含空格,任何有效字符对于常规标识符,以及任何一个以下字符.
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 (`)
马克
相关文章