在ksh中,将参数传递给mysql select查询

时间:2014-01-17 20:24:36

标签: mysql ksh

我有一个获取变量的linux脚本,我将它存储到var JOB_EXEC_ID 我试图将此值传递给MySQL查询

这是MySQL查询设置

print "JOB EXEC ID value for DataMeer Job ${LOADJOB} is : ${JobExecId} " |
  tee -a ${LOGDIR}/${LOGFILE}

#Log on to MySQL to get the DataId
#Remove first the output file that would house the dataid

rm -f ${SCRDIR}/list_dataid.csv

mysql -u root -pmonday1 ${DAPDBNAME} < ${SCRDIR}/dataid_query.nosql 
SQLRTN=$?
if [[ ${SQLRTN} != 0 ]]
then
  print "Return code from sqlcall - DAP : ${SQLRTN}" |
    tee -a ${LOGDIR}/${LOGFILE}
  print "Exiting $Script - 55 " |
    tee -a ${LOGDIR}/${LOGFILE}
  exit 55
fi

文件dataid_query.nosql如下所示:

set @job_exec_id=10151
select d.id DataId
from data d inner join dap_job_configuration djc on d.dap_job_configuration__id = djc.id
left outer join dap_job_execution dje on djc.id = dje.dap_job_configuration__id and dje.created_data__id = d.id
where dje.id=@job_exec_id
into OUTFILE "/home/app1ebb/cs/list_dataid.csv"

我想将JOB_EXEC_ID的值传递给当前硬编码的set命令,值为10151

1 个答案:

答案 0 :(得分:0)

取代

mysql -u root -pmonday1 ${DAPDBNAME} < ${SCRDIR}/dataid_query.nosql
SQLRTN=$?

这一行

sed "1 s/[0-9]*$/${JOB_EXEC_ID}/" > /tmp/dataid_query.nosql
mysql -u root -pmonday1 ${DAPDBNAME} < /tmp/dataid_query.nosql
SQLRTN=$?
rm /tmp/dataid_query.nosql