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

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

问题描述

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



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

解决方案

link 应为:

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

注意,一般来说,你必须将表的名称嵌入 [...] ,因为规则3(因此 SELECT * FROM [SET] 是有效的查询, c $ c> SET 是保留关键字,您可以使用 [...]
$ b

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



https://msdn.microsoft.com/zh-TW/library/ms175874.aspx


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

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


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


< blockquote>

我写的规则是完整表,不是临时表,不包括模式名。


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

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

解决方案

The regex described in the link should be:

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

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:

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

  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.

And they forgot: 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.

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天全站免登陆