Skip to content

bobby_dreamer

MySQL User-defined variables(UDV)

mysql1 min read

MySQL UDV's are similar to variables used in programs. It can hold only single value. Prefixed with @ before the variable name. You can use UDV without declaring or initializing it. If its not declared or initialized, it will have NULL


MySQL variable assignment

Method 1

Method 2


View the stored variable value

At any point in time, it can hold only one value

Where this could be useful ? (Usecases)
  1. Searching for same value in multiple tables or operations.
  1. Any type of complex ranking or any counters which you would like to generate.
    In the below query, @rank is getting initialized in the FROM clause like a join and incremented and SET in the SELECT clause