PASSWORD RESET

Your destination for complete Tech news

How to declare a variable in MySQL?

724 0
< 1 min read

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.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.