Home > Database > Mysql Tutorial > How Can I Pass Parameters to MySQL Scripts from the Command Line?

How Can I Pass Parameters to MySQL Scripts from the Command Line?

Susan Sarandon
Release: 2024-11-24 01:06:11
Original
637 people have browsed it

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;
Copy after login

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}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template