In MySQL, you can declare a variable by using the DECLARE
statement. Here is the syntax:
DECLARE var_name [, var_name] ... datatype [DEFAULT value];
For example, to declare a variable x
of type INT
with a default value of 0
, you can use the following statement:
DECLARE x INT DEFAULT 0;
You can also declare multiple variables in a single DECLARE
statement, like this:
DECLARE x INT DEFAULT 0, y VARCHAR(255) DEFAULT 'hello';
Once a variable is declared, you can assign a new value to it using the SET
statement. For example:
SET x = 10;
SET y = 'world';
Note that variables in MySQL are only valid within the current session and are not stored permanently. They are often used to store intermediate results or to pass values between different parts of a stored procedure or function.