MySQL: use environment variables in script

Is it possible to use an sql script variable defined externally?

eg. I have the following script:

UPDATE mytable SET valid = 0 WHERE valid = 1 

which I have to execute through the mysql command line several times, each with a different table name.

I would like something like:

 SET table_name=foo mysql -uuser -ppassword < myscript.sql 

Is it possible?

+6
source share
1 answer

Getting around environment variables, why not:

 sed 's/mytable/foo/' myscript.sql | mysql -uuser -ppassword 
+5
source

Source: https://habr.com/ru/post/886470/


All Articles