用于验证 SQL Server 表名的正则表达式 [英] Regular expression for validating SQL Server table name

查看:17
本文介绍了用于验证 SQL Server 表名的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 代码创建动态 SQL Server 表,但我需要验证表名.

I am creating dynamic SQL Server table using C# code but I need to validate the table name.

什么是用于验证 SQL Server 表名的正则表达式?

What is a regular expression for validating SQL Server table names?

推荐答案

link 应该是:

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

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

Note that in general you'll have to embed the name of the table in [...], because of the rule 3 (so SELECT * FROM [SET] is a valid query, because, while SET is a reserved keyword, you can "escape" it with the [...])

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

Note that in the linked page the rule is incomplete:

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

  1. 标识符不能是 Transact-SQL 保留字.SQL Server 保留了保留字的大写和小写版本.在 Transact-SQL 语句中使用标识符时,不符合这些规则的标识符必须用双引号或括号分隔.保留的字取决于数据库兼容级别.可以使用 ALTER DATABASE 语句设置此级别.
  1. The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words. When identifiers are used in Transact-SQL statements, the identifiers that do not comply with these rules must be delimited by double quotation marks or brackets. The words that are reserved depend on the database compatibility level. This level can be set by using the ALTER DATABASE statement.

他们忘记了:https://msdn.microsoft.com/en-我们/图书馆/ms174979.aspx

是新表的名称.表名必须遵循标识符规则.table_name 最多可以包含 128 个字符,但本地临时表名(名称以单个数字符号 (#) 为前缀)不能超过 116 个字符.

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.

我编写的规则是针对完整"表的,而不是针对临时表的,并且不包括架构名称.

The rule that I've written is for "full" tables, not for temporary tables, and doesn't include schema name.

这篇关于用于验证 SQL Server 表名的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆