Home >Database >Mysql Tutorial >How to Pass Parameters to MySQL Scripts via Command Line?

How to Pass Parameters to MySQL Scripts via Command Line?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-13 07:19:02404browse

How to Pass Parameters to MySQL Scripts via Command Line?

Passing Parameters to MySQL Script via Command Line

Passing parameters from the command line to a MySQL script enables dynamic execution of scripts with variable inputs. One method involves utilizing MySQL user-defined variables.

To pass a parameter named @start_date to a script, use the following syntax:

mysql –uuser_id -ppassword –h mysql-host -A \
-e "set @start_date=${start_date}; source ${sql_script};" >${data_file};

For example, to run a query that filters customer data based on a start and end date, you can execute the following command:

/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}

This method allows you to dynamically pass parameters to your MySQL scripts from the command line, making them more versatile and easier to use in automated workflows.

The above is the detailed content of How to Pass Parameters to MySQL Scripts via Command Line?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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