Key Features of a User-Defined Variable

  • A User-Defined Variable is session specific i.e variable defined by one client is not shared to other client and when the session ends these variables are automatically expired.
  • These variables are not case-sensitive. So, @mark or @Mark both refer to same value.
  • Maximum length of variables can be 64 characters.
  • Variable name can include other characters like- {!, #, ^, -, ..} in its name, if they are quoted. For ex- @’var@1′ or @”var^2″ or @`var3`.
  • These variables can’t be declared, they are only initialized i.e at time of declaration they should be assigned a value.
  • An undeclared variable can also be accessed in a SQL statement but their values is set as NULL.
  • These variables can take values from the following set of datatypes- { integer, floating-point, decimal, binary, nonbinary string or NULL value}.

User-Defined Variables

User-Defined Variables in are a way to store temporary values that can be used within a session. These variables are unique to a session and are not persistent across multiple sessions. They can be very useful for storing intermediate results or values that you want to reuse within a single session.

MySQL also supports the concept of User-defined variables, which allows passing of a value from one statement to another. A user-defined variable in MySQL is written as @var_name where, var_name is the name of the variable and can consist of alphanumeric characters, ., _, and $.

Similar Reads

Key Features of a User-Defined Variable

A User-Defined Variable is session specific i.e variable defined by one client is not shared to other client and when the session ends these variables are automatically expired. These variables are not case-sensitive. So, @mark or @Mark both refer to same value. Maximum length of variables can be 64 characters. Variable name can include other characters like- {!, #, ^, -, ..} in its name, if they are quoted. For ex- @’var@1′ or @”var^2″ or @`var3`. These variables can’t be declared, they are only initialized i.e at time of declaration they should be assigned a value. An undeclared variable can also be accessed in a SQL statement but their values is set as NULL. These variables can take values from the following set of datatypes- { integer, floating-point, decimal, binary, nonbinary string or NULL value}....

Syntax

SET @var_name = expression...

Examples

1. Assigning value to a variable using SET command....

User-Defined Variables – FAQs

What is an example of a user variable?...