MySQL/Language/User Variables

< MySQL < Language

Local variables

The local variables can't be reached from outside their function or stored procedure[1].

They are declared like this[2]:

DECLARE MyVariable1 INT DEFAULT 1;

Session variables

 select @test := 2;
 select @test + 1;  -- returns 3

 set @startdate='some_start_date', @enddate='some_end_date'

 SELECT @toremember:=count(*) FROM membros;
	
 select @numzero := count(*) from table1 where field=0; 
 select @numdistinct := count(distinct field) from table1 where field <> 0 ; 
 select @numzero @numdistinct;
 SET @id = 0, @name = '';
 SELECT id, name INTO @id, @name FROM table1 limit 1;
 SELECT @id, @name;

Global variables

A global variable is visible to all users, it allows to modify the configuration files settings during the session or definitely. So when changing them, it's necessary to precise this permanent or ephemera criteria, with respectively set global and set session. Example:

 mysql> set @@global.max_connections = 1000;
 mysql> show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 60 |
+---------------+-------+
1 row in set (0.00 sec)
 mysql> set @@session.wait_timeout=120;

References

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.