用于验证 SQL 服务器 table 名称的正则表达式

Regular expression for validating SQL Server table name

我正在使用 C# 代码创建动态 SQL 服务器 table,但我需要验证 table 名称。

什么是验证 SQL 服务器 table 名称的正则表达式?

link 中描述的正则表达式应该是:

var regex = new Regex(@"^[\p{L}_][\p{L}\p{N}@$#_]{0,127}$");

请注意,由于规则 3(因此 SELECT * FROM [SET] 是一个有效的查询,因为,虽然 SET 是保留关键字,但您可以使用 [...])

"escape"

请注意,链接页面中的规则不完整:

来自https://msdn.microsoft.com/en-us/library/ms175874.aspx

  1. 标识符不能是 Transact-SQL 保留字。 SQL 服务器保留保留字的大写和小写版本。在Transact-SQL语句中使用标识符时,不符合这些规则的标识符必须用双引号或括号分隔。保留的字取决于数据库兼容级别。可以使用 ALTER DATABASE 语句设置此级别。

他们忘记了:https://msdn.microsoft.com/en-us/library/ms174979.aspx

Is the name of the new table. Table names must follow the rules for identifiers. table_name can be a maximum of 128 characters, except for local temporary table names (names prefixed with a single number sign (#)) that cannot exceed 116 characters.

我写的规则是针对 "full" tables,而不是临时 tables,并且不包括架构名称。