Insert statement with variable
mysql>
mysql> CREATE TABLE VARIABLES
-> (VARNAME CHAR(30) NOT NULL PRIMARY KEY,
-> VARVALUE CHAR(30) NOT NULL);
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> SET @VAR1 = 100, @VAR2 = 'John';
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> INSERT INTO VARIABLES VALUES ('VAR1', @VAR1);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO VARIABLES VALUES ('VAR2', @VAR2);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> SELECT @VAR1 := VARVALUE
-> FROM VARIABLES
-> WHERE VARNAME = 'VAR1';
+-------------------+
| @VAR1 := VARVALUE |
+-------------------+
| 100 |
+-------------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT @VAR2 := VARVALUE
-> FROM VARIABLES
-> WHERE VARNAME = 'VAR2';
+-------------------+
| @VAR2 := VARVALUE |
+-------------------+
| John |
+-------------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT @VAR1, @VAR2;
+-------+-------+
| @VAR1 | @VAR2 |
+-------+-------+
| 100 | John |
+-------+-------+
1 row in set (0.00 sec)
mysql>
mysql> drop table variables;
Query OK, 0 rows affected (0.00 sec)
Related examples in the same category