SQL 我如何使用 shell 脚本执行 oracle Query 并在电子邮件中发送结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30657357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i execute oracle Query using shell Scripting and send the results in the email
提问by sachin bhandari
I am new to Unix scripting.. I have an oracle DB which has a username, password, hostname, port and servicename. I want to connect the database and run the query and in the end the result should be email to me or other people. Result should be displayed in the body of an email.
我是 Unix 脚本的新手。我有一个 oracle DB,它有用户名、密码、主机名、端口和服务名。我想连接数据库并运行查询,最后结果应该是通过电子邮件发送给我或其他人。结果应显示在电子邮件正文中。
回答by Lalit Kumar B
This is a sample shell script to connect through SQL*Plus, SPOOLthe output and send it as emailin the message body.
这是一个示例 shell 脚本,用于通过SQL*Plus进行连接、SPOOL输出并将其作为邮件正文中的电子邮件发送。
#!/bin/sh -- or bash or ksh
sqlplus -s /nolog <<EOF
CONNECT username/password@sid
SPOOL /u01/spool.csv
--do something
SPOOL OFF
EXIT;
EOF
mail -s "Subject" [email protected] < /u01/spool.csv
回答by Rajesh
PFB functionality breakup to connect oracle, email query result(as body) using shell Script with example :
PFB 功能分解以使用 shell 脚本与示例连接 oracle、电子邮件查询结果(作为正文):
- Connect to Oracle Database
- Set Headers for formatted output
- Run sql query and store result to temp file(query_output.dat)
- 连接到 Oracle 数据库
- 为格式化输出设置标题
- 运行 sql 查询并将结果存储到临时文件(query_output.dat)
http://www.folkstalk.com/2012/06/connect-to-oracle-database-in-unix.html
http://www.folkstalk.com/2012/06/connect-to-oracle-database-in-unix.html
Send output as body. Ex
mailx -s "subject" mail_address <query_output.dat rm query_output.dat
将输出作为正文发送。前任
mailx -s "subject" mail_address <query_output.dat rm query_output.dat