How to declare a variable in mysql, so that my second query can use it?
I would like to write something like:
SET start = 1; SET finish = 10; SELECT * FROM places WHERE place BETWEEN start AND finish;
MySQL variable assignment There are two ways to assign a value to a user-defined variable. You can use either := or = as the assignment operator in the SET statement. For example, the statement assigns number 100 to the variable @counter. The second way to assign a value to a variable is to use the SELECT statement.
The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.
To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false).
-- Syntax to Declare/Set a Global variable SET GLOBAL sort_buffer_size = 500000; SET @@global. sort_buffer_size = 500000; -- Syntax to Declare/Set a Session variable SET sort_buffer_size = 500000; SET SESSION sort_buffer_size = 500000; SET @@sort_buffer_size = 500000; SET @@local.
There are mainly three types of variables in MySQL:
User-defined variables (prefixed with @
):
You can access any user-defined variable without declaring it or initializing it. If you refer to a variable that has not been initialized, it has a value of NULL
and a type of string.
SELECT @var_any_var_name
You can initialize a variable using SET
or SELECT
statement:
SET @start = 1, @finish = 10;
or
SELECT @start := 1, @finish := 10; SELECT * FROM places WHERE place BETWEEN @start AND @finish;
User variables can be assigned a value from a limited set of data types: integer, decimal, floating-point, binary or nonbinary string, or NULL value.
User-defined variables are session-specific. That is, a user variable defined by one client cannot be seen or used by other clients.
They can be used in SELECT
queries using Advanced MySQL user variable techniques.
Local Variables (no prefix) :
Local variables needs to be declared using DECLARE
before accessing it.
They can be used as local variables and the input parameters inside a stored procedure:
DELIMITER // CREATE PROCEDURE sp_test(var1 INT) BEGIN DECLARE start INT unsigned DEFAULT 1; DECLARE finish INT unsigned DEFAULT 10; SELECT var1, start, finish; SELECT * FROM places WHERE place BETWEEN start AND finish; END; // DELIMITER ; CALL sp_test(5);
If the DEFAULT
clause is missing, the initial value is NULL
.
The scope of a local variable is the BEGIN ... END
block within which it is declared.
Server System Variables (prefixed with @@
):
The MySQL server maintains many system variables configured to a default value. They can be of type GLOBAL
, SESSION
or BOTH
.
Global variables affect the overall operation of the server whereas session variables affect its operation for individual client connections.
To see the current values used by a running server, use the SHOW VARIABLES
statement or SELECT @@var_name
.
SHOW VARIABLES LIKE '%wait_timeout%'; SELECT @@sort_buffer_size;
They can be set at server startup using options on the command line or in an option file. Most of them can be changed dynamically while the server is running using SET GLOBAL
or SET SESSION
:
-- Syntax to Set value to a Global variable: SET GLOBAL sort_buffer_size=1000000; SET @@global.sort_buffer_size=1000000; -- Syntax to Set value to a Session variable: SET sort_buffer_size=1000000; SET SESSION sort_buffer_size=1000000; SET @@sort_buffer_size=1000000; SET @@local.sort_buffer_size=10000;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With