Note that there are some explanatory texts on larger screens.

plurals
  1. POValidate MSSQL identifier (parameter) in .NET or regular expression
    text
    copied!<p>In a .NET project I need to verify if a string is a valid Microsoft SQL Server 2005 parameter identifier.</p> <p><em>Example:</em> <code>SELECT * FROM table WHERE column = @parameter</code></p> <p><strong>Is there a runtime class method to validate a string for being a parameter, or is there a regular expression that verifies the rules?</strong> (see below)</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms175874(SQL.90).aspx" rel="nofollow noreferrer">From the documentation on identifiers</a>, parameters should comply to these general identifier rules:</p> <blockquote> <ol> <li>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 (#).<br> 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 @@.</li> <li>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.</li> <li>The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words.</li> <li>Embedded spaces or special characters are not allowed.</li> <li>Supplementary characters are not allowed.</li> </ol> <p>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.</p> </blockquote> <p>Since I want to validate parameters only, identifiers must start with an @ sign, and must not be delimited.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload