gpt4 book ai didi

.net - 验证 .NET 或正则表达式中的 MSSQL 标识符(参数)

转载 作者:行者123 更新时间:2023-12-02 03:23:27 25 4
gpt4 key购买 nike

在 .NET 项目中,我需要验证字符串是否是有效的 Microsoft SQL Server 2005 参数标识符。

示例: SELECT * FROM table WHERE column = @parameter

是否有运行时类方法来验证字符串是否为参数,或者是否有正则表达式来验证规则?(见下文)

From the documentation on identifiers ,参数应符合这些通用标识符规则:

  1. 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 @@.
  2. 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.
  3. The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words.
  4. Embedded spaces or special characters are not allowed.
  5. Supplementary characters are not allowed.

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.

由于我只想验证参数,因此标识符必须以 @ 符号开头,并且不能分隔。

最佳答案

我在 Unicode 字符类上遇到了困难,但是当我发现 .NET 正则表达式支持它们时,我想出了以下正则表达式来解决我的问题:

@[\p{L}{\p{Nd}}$#_][\p{L}{\p{Nd}}@$#_]*

这强制执行:

  • 标识符始终以 @ 开头,使其成为参数。
  • 第二个位置不允许有 @,以避免与特殊 TSQL 函数混淆。
  • 仅允许使用规则中定义的字符。

关于.net - 验证 .NET 或正则表达式中的 MSSQL 标识符(参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/775587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com