[英]How to run SQL in shell script
How do you run a SQL command in a shell script while setting a variable?如何在设置变量时在 shell 脚本中运行 SQL 命令? I had tried this method and it isn't executing the command, it is thinking the command is just a string.我试过这种方法,它没有执行命令,它认为命令只是一个字符串。
#!/bin/ksh
variable1=`sqlplus -s username/pw@oracle_instance <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
select count(*) from table;
EXIT;
EOF`
if [ -z "$variable1" ]; then
echo "No rows returned from database"
exit 0
else
echo $variable1
fi
#!/bin/ksh
variable1=$(
echo "set feed off
set pages 0
select count(*) from table;
exit
" | sqlplus -s username/password@oracle_instance
)
echo "found count = $variable1"
Maybe it's too late for answering but, there's a working code:也许回答为时已晚,但是有一个有效的代码:
sqlplus -s "/ as sysdba" << EOF
SET HEADING OFF
SET FEEDBACK OFF
SET LINESIZE 3800
SET TRIMSPOOL ON
SET TERMOUT OFF
SET SPACE 0
SET PAGESIZE 0
select (select instance_name from v\$instance) as DB_NAME,
file_name
from dba_data_files
order by 2;
sqlplus -s /nolog <<EOF
whenever sqlerror exit sql.sqlcode;
set echo on;
set serveroutput on;
connect <SCHEMA>/<PASS>@<HOST>:<PORT>/<SID>;
truncate table tmp;
exit;
EOF
Code代码
PL_CONNECT_STRING="$DB_USERNAME/$DB_PASSWORD@$DB_SERVICE"
OUTPUT=$(sqlplus -s $PL_CONNECT_STRING <<-END-OF-SQL
select count(*) from table;
exit;
END-OF-SQL)
echo "COMPLETED GATHER STATS $OUTPUT";
Explanation:说明:
PL_CONNECT_STRING
carry database username, password and it service name PL_CONNECT_STRING
携带数据库用户名、密码和服务名
sqlplus is used to connect the Database with PL_CONNECT_STRING
details sqlplus 用于连接带有PL_CONNECT_STRING
详细信息的数据库
END-OF-SQL
tag contain the query which you want to execute END-OF-SQL
标记包含您要执行的查询
echo
is used to print the output of the query echo
用于打印查询的输出
NOTE: You can give multiple Query inside the END-OF-SQL
tag, so its useful for batch execution as well注意:您可以在END-OF-SQL
标签内提供多个 Query,因此它对批处理执行也很有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.