How do you pass command line arguments into SQL scripts as session variables from mysql or singlestore client?

We recently got an inquiry from a customer asking “How do you pass command line arguments into SQL scripts as session variables from mysql or singlestore client?”

I’ll answer it below.

You can do this like so:

sh-4.4# singlestore -e "set @x = 3; source /foo.sql;" -p
Enter password: 
+---+
| y |
+---+
| 2 |
+---+
+---------------+
| another_x_val |
+---------------+
|             3 |
+---------------+
sh-4.4# cat foo.sql
select 2 as y;
select @x as another_x_val;

That is, you can use the -e option to pass a string that assigns values to session variables, and optionally also sources a file of SQL commands. Those commands can refer to the variables.