简体   繁体   English

将假脱机文件的名称从 shell 脚本传递给 sqlplus

[英]Passing the name of spool file to sqlplus from a shell script

I am trying to write a unix program, in which I need to connect to the SQL DB and fetch the data and store it into a file.我正在尝试编写一个 unix 程序,在该程序中我需要连接到 SQL DB 并获取数据并将其存储到一个文件中。

Currently I am using the following command:目前我正在使用以下命令:

output1=`sqlplus -s username@SID/password <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING ON ECHO OFF;
SPOOL EMP_NAMES.txt
select emp_name from employee order by emp_name;
Spool off;

This is working fine.这工作正常。 But my requirement was that I want to pass the value of spool file such that everytime a new Spool file would be generated.但我的要求是我想传递假脱机文件的值,这样每次都会生成一个新的假脱机文件。

I basically want to append the date at the end of the file name like:我基本上想在文件名的末尾附加日期,例如:

date=`date +'%d/%m/%Y_%H:%M:%S:%2N'`
output1=`sqlplus -s username@SID/password <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING ON ECHO OFF;
SPOOL EMP_NAMES_$date.txt

Kindly let me know as to how this can be done.请让我知道如何做到这一点。

If you call your sqlplus with a heredoc, you can do this easily:如果你用 heredoc 调用你的sqlplus ,你可以很容易地做到这一点:

spool_file=: ... your date logic here ...
sql_ouput=: ... path for sqlplus output & errors ...
sqlplus -s username@SID/password << EOF &> "$sql_output"
  SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING ON ECHO OFF;
  spool $spool_file
  # SQL statements
  spool off
EOF
if [[ $? != 0 ]]; then
  : ... error handling ...
fi
  • It's better to capture stdout/stderr of sqlplus into a file rather than a shell variable.最好将sqlplus stdout/stderr 捕获到文件而不是 shell 变量中。
  • I think it is possible to hide the password by removing it from the command line and adding it as the first line of heredoc (this will prevent password from showing in ps )我认为可以通过从命令行中删除密码并将其添加为 heredoc 的第一行来隐藏密码(这将防止密码显示在ps

Take a look at this related post: Connect to sqlplus in a shell script and run SQL scripts看看这个相关的帖子: Connect to sqlplus in a shell script and run SQL scripts

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

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