Pass parameters to MySQL script

This may work for you

mysql -u root -p -e"set @temp=1;" < /home/mysql/Desktop/a.sql

and

mysql> set @temp=some_value;
mysql> source file.sql

this almost similar to your problem just try it


You can use user variables to achieve the behaviour you describe. As you use the variable as a schema identifier, not a data value, you'll have to use a prepared statement so you can compose the query dynamically.

query1.sql:

SET @query = CONCAT('Select * FROM ', @tblName, ' LIMIT 10');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Invoked as

mysql> SET @tblName = 'Users'; \. query1.sql

The command line call from vidyadhar worked for me, but only with a small modification :

mysql -u root -p -e"set @temp=1; `cat /home/mysql/Desktop/a.sql`"

Tags:

Mysql