简体   繁体   English

如何将 sqlplus 参数从 shell 脚本传递到 sql 文件

[英]How to Pass a sqlplus parameter from shell script to sql file

I am having a Shell script that has a variable storing the date value using sqlplus connection as below.我有一个 Shell 脚本,它有一个使用 sqlplus 连接存储日期值的变量,如下所示。 I want to pass the shell variable 'var_Date' value to the sql file.我想将 shell 变量“var_Date”值传递给 sql 文件。

cat extract.sh猫提取物.sh

#!/bin/ksh
var_Date=`sqlplus -s $DB_USER/$DB_PASS@$DB_HOST:$DB_PORT/$DB_SID << EOF
SELECT MAX(SAMPLE_DATE)-1 FROM SAMPLE_CASES WHERE KEY IN ('ab','bc');
EXIT;
EOF`
export var_Date
echo $var_Date

sqlplus -s $DB_USER/$DB_PASS@$DB_HOST:$DB_PORT/$DB_SID @data_extract.sql $var_Date

cat extract.sql猫提取物.sql

set echo off
set trimspool on
set pagesize 0
set colsep ~

spool extract.csv
SELECT CASE_ID FROM SAMPLE_CASE1 WHERE TIME_START>='&1'
spool off

I have tried to execute this script but it is failing with invalid identifier .我已尝试执行此脚本,但由于标识符无效而失败。 Please help to understand what is the mistake I am doing here.请帮助了解我在这里做的错误是什么。

After searching in the internet for almost a week, could arrive at a working code.在互联网上搜索了将近一周后,可以得到一个工作代码。 Below is the working code.下面是工作代码。

cat extract.sh猫提取物.sh

#!/bin/ksh
   var_Date=`sqlplus -s $DB_USER/$DB_PASS@$DB_HOST:$DB_PORT/$DB_SID << EOF
   SET HEADING OFF
   SELECT ''''||TRIM(MAX(SAMPLE_DATE)-1)||'''' FROM SAMPLE_CASES WHERE KEY IN ('ab','bc');
   EXIT;
   EOF`
   export var_Date
   echo $var_Date

   sqlplus -s $DB_USER/$DB_PASS@$DB_HOST:$DB_PORT/$DB_SID @data_extract.sql $var_Date

cat extract.sql猫提取物.sql

set echo off
set trimspool on
set pagesize 0
set colsep ~

spool extract.csv
SELECT CASE_ID FROM SAMPLE_CASE1 WHERE to_char(TIME_START,'DD-MON-YYYY') >='&1'
spool off

Firstly, we to need to pass the argument $var_Date to sqlplus within single quotes which is done within the variable declaration itself by concatenating ''''.首先,我们需要在单引号内将参数 $var_Date 传递给 sqlplus,这是通过连接 '''' 在变量声明本身内完成的。 We also need to use SET HEADING OFF to make the variable var_Date hold only the date value and not the header value.我们还需要使用 SET HEADING OFF 来使变量 var_Date 只保存日期值,而不是 header 值。

Secondly, there was a ora error: " ORA-01858: a non-numeric character was found where a numeric was expected ".其次,出现了一个 ora 错误:“ ORA-01858:在应有数字的地方发现了一个非数字字符”。 To mitigate this, I had type-casted the field time_start to to_char post which the filter is applied and stored in the csv file.为了缓解这种情况,我将字段 time_start 类型转换为 to_char post,过滤器被应用并存储在 csv 文件中。

Hope this helps others who had trouble like me.希望这可以帮助像我一样遇到麻烦的其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM