将shell变量传递给SQL语句
我想把shell变量传递给sql语句。shell脚本和SQL语句都存在于同一脚本文件中。 我想在SQL语句中输入变量retMonth、retLastDay和retPrvYear的值。 下面是代码 如果我执行此操作,它将在'01-'和'-'\0 0]1 1 1]12-DEC-14 1之间打印-partition_date 如何在SQL语句中具有retMonth、retLastDay和retPrvYear的值将shell变量传递给SQL语句,sql,shell,sqlplus,Sql,Shell,Sqlplus,我想把shell变量传递给sql语句。shell脚本和SQL语句都存在于同一脚本文件中。 我想在SQL语句中输入变量retMonth、retLastDay和retPrvYear的值。 下面是代码 如果我执行此操作,它将在'01-'和'-'\0 0]1 1 1]12-DEC-14 1之间打印-partition_date 如何在SQL语句中具有retMonth、retLastDay和retPrvYear的值 echo $retMonth //This prints 07 echo $retLas
echo $retMonth //This prints 07
echo $retLastDay //This prints 31
echo $retPrvYear //This prints 2015
count=$(sqlplus -s ${DBA_ORACLE_USER}/${DBA_ORACLE_PWORD}@${ORACLE_SID} <<END
#connect ${DBA_ORACLE_USER}/${DBA_ORACLE_PWORD}@${ORACLE_SID}
set serveroutput on
set linesize 1000
set heading off
set feedback off
define lastMonth=$retMonth
define lastYear=$retPrvYear
define lastDay=$retLastDay
SELECT count(1)
FROM MYTABLE
WHERE partition_date between '01-$lastMonth-$lastYear' and '$lastDay-$lastMonth-$lastYear'
);
END
)
尝试直接使用带引号的shell变量,而不使用define指令:
count=$(sqlplus -s "${DBA_ORACLE_USER}/${DBA_ORACLE_PWORD}@${ORACLE_SID}" <<END
set serveroutput on
set linesize 1000
set heading off
set feedback off
SELECT count(1)
FROM MYTABLE
WHERE partition_date between
"01-$retMonth-$retPrvYear" and "$retLastDay-$retMonth-$retPrvYear";
END
)
即使是双qoutes也不起作用。在某种程度上,它给出了同样的结果。现在,它的打印和分区日期介于“2015年7月1日”和“2015年7月31日”之间\0 0]1 1 1]12-DEC-14 1。尝试更新的ans后,它现在打印和分区日期介于2015年7月1日和2015年7月31日\0 0]1]12-DEC-14 1之间。我不明白为什么要打印\0 0]1 1 1]12-DEC-14 1如果您只保留01-$retMonth-$retPrvYear和$retLastDay-$retMonth-$retPrvYear之间的时间,会打印什么