danieln danieln - 1 year ago 89
MySQL Question

Pass parameter to MySQL script command line

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

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

Answer Source

Found an answer on the net here.

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;

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}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download