简体   繁体   English

如何编写Shell脚本以将查询结果输出到文本文件

[英]How to write a shell script to output the result of a query to a text file

I want to write a shell script that will have a SQL select query and will output a text file. 我想编写一个具有SQL选择查询并输出文本文件的shell脚本。

My server is Linux, database is oracle. 我的服务器是Linux,数据库是oracle。

I connect to my database by: 我通过以下方式连接到我的数据库:

putty- su - oracle
. .profile_1120_rdbms
sqlplus SYSTEM/SYADM
sql> here i write sql query

But i want my query to run from shell script, which run as root user. 但是我希望我的查询从以root用户身份运行的shell脚本运行。

I do not know which environments to set /do i need any drivers installed. 我不知道要设置哪个环境/我是否需要安装任何驱动程序。

I have tried some pieces of code, but they give me sqlplus not found /invalid command . 我已经尝试了一些代码,但是它们给了我sqlplus not found /invalid command I am not able to connect to db. 我无法连接到数据库。

Please suggest me if there is some other way... I just want to run my sql from some script and have output in some text file. 请建议我是否还有其他方法...我只想从某些脚本运行sql并在某些文本文件中输出。

You can give sql*plus a script filename with @file -- 您可以使用@file为sql * plus提供脚本文件名-

sqlplus scott/tiger @query.sql

For outputting just the query results, I've used commands like these: 为了仅输出查询结果,我使用了以下命令:

set echo off;
set feedback off;
set linesize 100;
set pagesize 0;
set sqlprompt '';
set trimspool on;
spool result.txt;

at the beginning to the query.sql file to suppress headings, prompts, etc. 在query.sql文件的开头以隐藏标题,提示等。

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

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