Home >Database >Mysql Tutorial >How Can I Pass Parameters to MySQL Scripts from the Command Line?
Passing Parameters to MySQL Script Command Line
It is possible to pass parameters from the command line to MySQL scripts. This can be useful for passing dynamic values or user-specified inputs into the script.
Pass Parameters Using Set Variables
One method to pass parameters is to use set variables within the script. These variables are assigned values on the command line and can be accessed inside the script using the @ symbol.
For instance, suppose you want to run a query that filters customer data based on a start and end date range:
Select c_id, c_first_name,c_last_name, c_address,last_modified_date from customer where last_modified_date >=@start_date and last_modified_date <= @end_date;
To pass the start and end dates from the command line, enter the following:
/usr/bin/mysql –uuser_id -ppassword –h mysql-host -A \ -e "set @start_date=${start_date}; set @end_date=${end_date};\ source ${sql_script};" > ${data_file}
Here, @start_date and @end_date are set to the corresponding command line variables ${start_date} and ${end_date}. Inside the script, you can then access these variables to dynamically filter the query results.
The above is the detailed content of How Can I Pass Parameters to MySQL Scripts from the Command Line?. For more information, please follow other related articles on the PHP Chinese website!