Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a variable to a mysql script?

Tags:

I know that with mysql you can write SQL statements into a .sql file and run the file from the mysql command line like this:

mysql> source script.sql 

How do I pass a variable to the script? For example, if I want to run a script that retrieves all the employees in a department, I want to be able to pass in the number of the department as a variable.

I am not trying to run queries through a shell script. There are simple queries I run from the mysql command line. I'm tired of retyping them all the time, and writing a shell script for them would be overkill.

like image 281
Tom Burke Avatar asked Sep 16 '08 19:09

Tom Burke


People also ask

How do you pass variables in query?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.

How do I assign a variable in MySQL?

MySQL variable assignment There are two ways to assign a value to a user-defined variable. You can use either := or = as the assignment operator in the SET statement. For example, the statement assigns number 100 to the variable @counter. The second way to assign a value to a variable is to use the SELECT statement.

Can you use variables in MySQL?

Mysql also supports the concept of User-defined variables, which allows passing of a value from one statement to another. A user-defined variable in Mysql is written as @var_name where, var_name is the name of the variable and can consist of alphanumeric characters, ., _, and $.

How do I use a variable in SQL SELECT statement?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.


2 Answers

Like this:

set @department := 'Engineering'; 

Then, reference @department wherever you need to in script.sql:

update employee set salary = salary + 10000 where department = @department; 
like image 174
Brad Choate Avatar answered Sep 22 '22 19:09

Brad Choate


    #!/bin/bash      #verify the passed params     echo 1 cmd arg : $1     echo 2 cmd arg : $2      export db=$1     export tbl=$2      #set the params ... Note the quotes ( needed for non-numeric values )     mysql -uroot -pMySecretPaassword \     -e "set @db='${db}';set @tbl='${tbl}';source run.sql ;" ;      #usage: bash run.sh my_db my_table     #     #eof file: run.sh      --file:run.sql      SET @query = CONCAT('Select * FROM ', @db , '.' , @tbl ) ;     SELECT 'RUNNING THE FOLLOWING query : ' , @query ;     PREPARE stmt FROM @query;     EXECUTE stmt;     DEALLOCATE PREPARE stmt;      --eof file: run.sql 

you can re-use the whole concept from from the following project

like image 33
Yordan Georgiev Avatar answered Sep 20 '22 19:09

Yordan Georgiev