I am trying to pass parameters from a shell script to an sql file, to be used in the MySQL queries contained in said sql file. What I am currently able to do is set the variable at the beginning of the sql file like so:
SET @name := 'Helen';
insert into testMe values (@name);
And then execute the sql file from my shell script like so:
#!/bin/bash
SQL_FILE="test.sql"
DB_HOST=$1
MYSQL_CONTAINER=$2
docker exec -i $MYSQL_CONTAINER sh -c "mysql -h $DB_HOST -u user -ppassword -D dbname" < "$SQL_FILE"
This will successfully insert a new row with Helen as the value.
mysql> select * from testMe;
+-------+
| name |
+-------+
| Helen |
+-------+
What I would like to do instead, though, is pass the variable @name
from the shell script file itself, rather than define it at the top of the sql file. Can anyone tell me how I can achieve that?