Setting custom variable

In MatrixOne, you can customize variables using the SET command and use them in SQL. The specific syntax is as follows:

  1. SET variable = expr, [variable = expr ..,]

Example

Now let’s define two variables, a and b:

  1. > SET @a=2, @b=3;
  2. Query OK, 0 rows affected (0.00 sec)
  3. > select @a;
  4. +------+
  5. | @a |
  6. +------+
  7. | 2 |
  8. +------+
  9. 1 row in set (0.00 sec)
  10. > select @b;
  11. +------+
  12. | @b |
  13. +------+
  14. | 3 |
  15. +------+
  16. 1 row in set (0.00 sec)

Using custom variables in SQL:

  1. > create table t1(a int,b varchar(1));
  2. Query OK, 0 rows affected (0.02 sec)
  3. > insert into t1 values(@a,@b);
  4. Query OK, 1 row affected (0.02 sec)
  5. > select * from t1;
  6. +------+------+
  7. | a | b |
  8. +------+------+
  9. | 2 | 3 |
  10. +------+------+
  11. 1 row in set (0.01 sec)

Note

The variables a and b are both ints here. If you want a string of 2 or 3, it is recommended to use SET @a ='2', @b='3';.