In order to create and assign a value to a variable, you use the SET statement. There are two variations of assigning a value to a variable:
- SET @varname = value;
- SET @varname := value;
You can also set a variable with a SELECT statement, as shown in the following code sample:
SELECT @varname := column1 FROM tablename;
You can name your variable what you like. Variable names are not case-sensitive. You can name it @varname and then use @VARname in a query when referencing the variable name, and it will work.