将参数传递给 MySQL 脚本命令行

2021-11-20 00:00:00 cmd mysql

是否有任何选项可以将参数从命令行传递到 MySQL 脚本?

Is there any option to pass a parameter from the command line to MySQL script?

在这个例子中类似于@start_date:

Something like @start_date in this example:

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

推荐答案

在网上找到了答案 此处.

基本上,假设我们要运行这个查询:

Basically, suppose that we want to run this query:

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;

我们可以像这样传递 'start_date' 和 'end_date':

We can pass 'start_date' and 'end_date' like this:

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

相关文章