Home > Database > Mysql Tutorial > body text

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

Susan Sarandon
Release: 2024-11-10 21:33:03
Original
392 people have browsed it

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

Passing Parameters to MySQL Script Command Line

Passing parameters from the command line to a MySQL script allows you to dynamically modify the script's behavior during execution. This is useful for automating scripts that require variable inputs.

One method to pass parameters is through the use of user-defined variables within the script. In the following example, we define the variable @start_date to hold the start date parameter:

set @start_date=${start_date};
Copy after login

We then call the script from the command line and assign the start date parameter to the variable:

/usr/bin/mysql –uuser_id -ppassword –h mysql-host -A \
    -e "set @start_date=${start_date}; source ${sql_script};" > ${data_file}
Copy after login

In this command, the -e option executes the specified SQL statement before executing the script. In our case, it sets the @start_date variable to the value of the start_date parameter passed from the command line.

Another option is to use the --parameter or -P flags in the following format:

mysql –uuser_id -ppassword –h mysql-host -A \
    -Pstart_date=${start_date} -Pend_date=${end_date} \
    source ${sql_script}; > ${data_file}
Copy after login

This method allows you to specify multiple parameters with their corresponding values. It's important to note that the -P flags must precede the source command in this case.

By utilizing either of these methods, you can effectively pass parameters to your MySQL scripts from the command line, enabling flexibility and automation in your SQL operations.

The above is the detailed content of How to 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