Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use special characters in SQL Server variables?

I'm trying to declare a variable like @wi-fi in SQL Server but dash character in not allowed. There are other characters like / * - ( ) with same problem. I know for table or column name we can put it in the [] but what about declaring variables? is there a solution?

I have already searched the web but couldn't find any way.

like image 329
Mahdi Hesari Avatar asked Dec 21 '25 19:12

Mahdi Hesari


1 Answers

Local variable names must comply with the rules for identifiers. MSDN says that:

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.

But this rule does not apply to variable naming. In SQL Server there is a restriction on variable names. All variable names must begin with a single @ sign. After that the variable name must follow the rules for identifiers and can contain a total of 128 characters. When we say characters, we mean that the name can contain:

  • Letters
  • Numbers
  • At sign @
  • Pound sign #
  • Dollar sign $
  • Underscore character _

The variable name cannot contain any dash or spaces.

like image 118
Vahid Farahmandian Avatar answered Dec 24 '25 11:12

Vahid Farahmandian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!