6

Is it possible to make a sql script use a variable defined externally?

E.g. I have the following script:

UPDATE mytable
SET    valid = 0
WHERE  valid = 1

which I have to run through 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?

Don
  • 16,928
  • 12
  • 63
  • 101

1 Answers1

5

Skirting around the environment variables, why not:

sed 's/mytable/foo/' myscript.sql | mysql -uuser -ppassword
VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97