【问题标题】:SPOOL in OracleOracle 中的 SPOOL
【发布时间】:2018-09-02 03:27:42
【问题描述】:

我有这个表和样本数据,我想将它们假脱机到一个文件中。 我不确定什么是正确的方法或语法。我尝试了几种来自互联网的方法,但无法得到正确的答案。

CREATE TABLE RoomType
(
RoomType Varchar2(25),
RoomTypeDesc Varchar2(50)
);

INSERT INTO RoomType VALUES ('STD', 'Standard Room');
INSERT INTO RoomType VALUES ('Business', 'Business Class');

【问题讨论】:

  • 您想将这些语句假脱机到一个文件(?),或执行它们的结果;或者你想查询表并将 那个 输出到一个文件?
  • @AlexPoole 我想查询我在 oracle 中所做的表并将输出假脱机到一个文件中。

标签: oracle oracle11g sqlplus


【解决方案1】:
SPOOL path/file/to/save/log.log(or txt)
PROMPT ************************************
PROMPT *** CREATION OF OBJECTS          ***
PROMPT ************************************
CREATE TABLE RoomType
(
RoomType Varchar2(25),
RoomTypeDesc Varchar2(50)
);
PROMPT ************************************
PROMPT *** INSERTION                    ***
PROMPT ************************************
INSERT INTO RoomType VALUES (STD, 'Standard Room');
INSERT INTO RoomType VALUES (Business, 'Business Class');

【讨论】:

    【解决方案2】:

    用你最喜欢的编辑器(比如vi)将你的命令保存在一个文件中,在你最喜欢的路径中(让我们把/home/oracle/scripts)作为.sql文件:

    [oracle@DoonieDB scripts]$ vi RoomTypes.sql
    set term[out] off
    set feed[back] off
    col RoomType format a25
    col RoomTypeDesc format a25
    spool /home/oracle/scripts/RoomTypes.txt
    select * from RoomType;
    spool off;
    

    其中 col 设置用于限制列的长度以使它们保持在单行中。

    连接到您的相关架构并通过以 @ 符号为前缀调用您的 .sql 并获取带有格式化信息的 RoomTypes.txt 文件:

    [oracle@DoonieDB scripts]$ pwd
    /home/oracle/scripts
    [oracle@DoonieDB scripts]$ sqlplus hotels/myKey
    ...    
    Connected to:
    Oracle Database xxx Enterprise Edition Release 1x.x.x.x.x - 64bit Production
    SQL> @RoomTypes.sql
    SQL> exit
    Disconnected from Oracle Database xxx Enterprise Edition Release 1x.x.x.x.x - 64bit Production
    [oracle@barbdb12c scripts]$ cat RoomTypes.txt
    
    ROOMTYPE                  ROOMTYPEDESC                                          
    ------------------------- -------------------------                             
    STD                       Standard Room                                         
    Business                  Business Class
    

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      相关资源
      最近更新 更多