SQLPlus - 使用假脱机创建View结构文件

时间:2017-11-01 19:26:09

标签: sql oracle csv sqlplus

Oracle版本:12c SQL Developer:4.1.1.19

我正在使用以下问题中的代码作为开头: SQLPlus - spooling to multiple files from PL/SQL blocks

我想创建一个过程:

  1. 将以下结果导出到单个文件(约110行)中 SPOOLING:

    SPOOL c:\temp\view_test.csv
    
    select 'desc '||view_name||';'FROM SYS.ALL_VIEWS WHERE OWNER = 'AN_OWNER_NAME' and substr(VIEW_NAME,1,2)='SN';
    
    SPOOL OFF
    
  2. 对于每个'DESC'行,创建一个带有'DESC'结果的csv文件

    desc SPECIFIC_VIEW_NAME;
    
  3. 每个视图的结果将如下所示(约110)

  4. Name                   Null     Type
    ---------------------- -------- -------------- 
    ID                     NOT NULL NUMBER(19)
    SYS_ID                 NOT NULL VARCHAR2(1000)
    ACTIVE                          VARCHAR2(1000)
    APRM_ID                         VARCHAR2(1000)
    APRM_KEY                        VARCHAR2(1000)
    BUSINESS_ONLY                   VARCHAR2(1000)
    

    这是我目前使用的代码。当我运行此代码时,系统会提示我输入dynamic_filename的值:

    set pagesize 0
    set trimspool on  
    set headsep off 
    set feedback off
    set echo off 
    set verify off
    set timing off
    set linesize 4000
    
    DEFINE rows_per_file = 50
    
    
    -- Create an sql file that will create the individual result files
    SET DEFINE OFF
    
    SPOOL c:\temp\generate_one.sql
    
    PROMPT COLUMN which_dynamic NEW_VALUE dynamic_filename
    PROMPT
    
    PROMPT SELECT 'c:\temp\run_#'||TO_CHAR( &1, 'fm000' )||'_result.txt' which_dynamic FROM dual
    PROMPT /
    
    PROMPT SPOOL &dynamic_filename
    
    PROMPT SELECT *
    PROMPT   FROM ( SELECT a.*, rownum rnum
    PROMPT            FROM ( select 'desc '||view_name||';'FROM SYS.ALL_VIEWS WHERE OWNER = 'AN_OWNER_NAME' and substr(VIEW_NAME,1,2)='SN' ) a
    PROMPT           WHERE rownum <= ( &2 * 50 ) )
    PROMPT  WHERE rnum >= ( ( &3 - 1 ) * 50 ) + 1
    PROMPT /
    
    PROMPT SPOOL OFF
    
    SPOOL OFF
    
    SET DEFINE &
    
    
    -- Define variable to hold number of rows
    -- returned by the query
    COLUMN num_rows NEW_VALUE v_num_rows
    
    -- Find out how many rows there are to be
    SELECT COUNT(*) num_rows
      FROM ( SELECT LEVEL num_files FROM dual CONNECT BY LEVEL <= 120 );
    
    
    -- Create a master file with the correct number of sql files
    SPOOL c:\temp\run_all.sql
    
    SELECT '@c:\temp\generate_one.sql '||TO_CHAR( num_files )
                                       ||' '||TO_CHAR( num_files )
                                       ||' '||TO_CHAR( num_files ) file_name
      FROM ( SELECT LEVEL num_files 
               FROM dual 
            CONNECT BY LEVEL <= CEIL( &v_num_rows / &rows_per_file ) )
    /
    
    SPOOL OFF
    
    -- Now run them all
    @c:\temp\run_all.sql
    

1 个答案:

答案 0 :(得分:0)

我问办公室周围有人建议我使用:

select TABLE_NAME,COLUMN_NAME, DATA_TYPE from SYS.ALL_TAB_COLS

这让我得到了我需要的东西。