23

I have a MySQL script file named query1.sql which contains:

select * FROM $(tblName) LIMIT 10;

I am in MySQL console, how do I pass the parameter to the script? This does not forward the variable:

mysql> \. query1.sql -v tblName=Users
1

6 Answers 6

18

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
Sign up to request clarification or add additional context in comments.

Comments

13

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`"

Comments

11

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

3 Comments

Here the first one does not work. The second one does.
@silverdr, what problem did you have with the first one?
@DavidWesby - I am sorry but it was five years ago… I honestly don't remember
2

For bash scripting:

tblName=$1 
searchFor="%something%"

echo "select * FROM $tblName LIMIT 10;
      select * From $tblName where somecolumn like '$searchFor';
" | mysql

For powershell:

$tblName="MyTable"
$searchFor="%something%"

"select * FROM $tblName LIMIT 10;
  select * From $tblName where somecolumn like '$searchFor';
" | mysql

Comments

1

The answer from @user4150422 above almost worked from me, but cat gave me permission issues so I had to use a slight alternative. Weirdly source command didn't work in place of cat either, but \. did

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

Comments

0

The advanced solution for use bash variables via mysql variables like @tblName in order to INSERT values into specific table:

The core file insert.preps included prepared statements procedural (SET > PREPARE > EXECUTE > DEALLOCATE):

SET @query = CONCAT("INSERT INTO ", @tblName, " (id, time, file, status) VALUES (?, ?, ?, ?)");
PREPARE stmt FROM @query;
SET @id = '0';
EXECUTE stmt USING @id, @time, @file, @status;
DEALLOCATE PREPARE stmt;

*SET @id = '0'; in the file predefined because id is usually is INT AUTO_INCREMENT PRIMARY KEY. It will auto-assign proper id for all lines.

Invoking it in the shell or in bash script with using variables:

mysql --defaults-extra-file=<(echo $'[client]\npassword='"$db_password") --user=$db_user $db_name -e "
     SET @tblName = '$table';
     SET @time = '$timeStamp';
     SET @file = '$file';
     SET @status = '$status';
`cat /path/to/insert.preps`"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.