MySQL 8.4.2
Source Code Documentation
mysqltest Variables

You can define variables and refer to their values.

You can also refer to environment variables, and there is a built-in variable that contains the result of the most recent SQL statement.

To define a variable, use the let command. Examples:

let $a= 14;
let $b= this is a string;
--let $a= 14
--let $b= this is a string

The variable name cannot contain whitespace or the “=” character.

If a variable has a numeric value, you can increment or decrement the value:

inc $a;
dec $a;
--inc $a
--dec $a

inc and dec are commonly used in while loops to modify the value of a counter variable that controls loop execution.

The result from executing a query can be assigned to a variable by enclosing the query within backtick (“`”) characters:

let $q= `select version()`;

References to variables can occur in the echo, eval, exec, and system commands. Variable references are replaced by their values. A nonquery value assigned to a variable in a let command can also refer to variables.

Variable references that occur within `query` are expanded before the query is sent to the server for execution.

You can refer to environment variables. For example, this command displays the value of the $PATH variable from the environment:

--echo $PATH

$mysql_errno is a built-in variable that contains the numeric error returned by the most recent SQL statement sent to the server, or 0 if the statement executed successfully. $mysql_errno has a value of −1 if no statement has yet been sent.

$mysql_errname similarly contains the symbolic name of the last error, or an empty string if there was no error.

mysqltest first checks mysqltest variables and then environment variables. mysqltest variable names are not case sensitive. Environment variable names are case sensitive.