Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the value of a variable at the commandline in MySQL?

I tried the following -

I created a variable at the command prompt as follows -

mysql> set @myId = 1; Query OK, 0 rows affected (0.00 sec) 

Then, to display it, I tried the following without success -

    mysql> show myId;     ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that  corresponds to your MySQL server version for the right syntax to use near 'myId' at line 1     mysql> show @myId;     ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@myId' at line 1     mysql> PRINT @myId;     ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that  corresponds to your MySQL server version for the right syntax to use near 'PRINT @myId' at line 1     mysql> PRINT myId;     ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that  corresponds to your MySQL server version for the right syntax to use near 'PRINT myId' at line 1 

So how can I display the value of @myId?

like image 408
CodeBlue Avatar asked Sep 16 '13 22:09

CodeBlue


People also ask

How do you display the value of a variable in MySQL?

SET @yourVariableName = yourValue; Let us check the above syntax to create a variable and display the value of created variable.

How do you reference a variable in MySQL?

User variables are written as @ var_name , where the variable name var_name consists of alphanumeric characters, . , _ , and $ . A user variable name can contain other characters if you quote it as a string or identifier (for example, @'my-var' , @"my-var" , or @`my-var` ).


2 Answers

Simply SELECT the variable like this:

SELECT @myId; 

Here is the MySQL documentation on user-defined variables:

http://dev.mysql.com/doc/refman/5.5/en/user-variables.html

like image 153
Mike Brant Avatar answered Sep 27 '22 19:09

Mike Brant


If you're looking for a variable that you set yourself like the OP did, then @MikeBrant's answer is correct:

SELECT @myId; 

But if you want to see the MySQL system variables (which is what I came here looking for), then you need to run:

show variables like '%slow%'; 

or possibly:

show global variables like '%slow%'; 
like image 39
Ryan Shillington Avatar answered Sep 27 '22 18:09

Ryan Shillington