Pass parameter to MySQL script command line

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}

Leave a Comment