简体   繁体   English

每个循环创建一个CSV文件| PLSQL Oracle SQL开发人员

[英]Creating a CSV file per Loop | PLSQL Oracle SQL Developer

Updated 更新

Rad-folks! 乡亲们!

TL;DR TL; DR

Need working code to loop through an existing list of all my tables that will create CSV files selecting the top 100 rows of each table. 需要工作代码来遍历我所有表的现有列表,这些列表将创建CSV文件,并选择每个表的前100行。 There are variables set up to capture the table names which will be used to dynamically, call the table for the select and name the file. 设置了一些变量来捕获表名,这些变量将用于动态地调用表,进行选择并命名文件。 Must be done through PLSQL and SQLDeveloper. 必须通过PLSQL和SQLDeveloper完成。 Do u kno da wae? 你知道吗?

Here is the situation: 情况如下:

  • Must gather all tables (base tables non temporary) and row count (row count>0) 必须收集所有表(非临时基表)和行数(行数> 0)
  • Create a loop to select(*) top 100 rows from the list of tables 创建一个循环以从表列表中选择(*)前100行
  • Take the result of the query and place it on a CSV file 获取查询结果并将其放在CSV文件中

Problems: 问题:

  • Declaring the Variables 声明变量
  • Using Begin and End 使用开始和结束
  • Using a dynamic name to produce unique CSV files 使用动态名称生成唯一的CSV文件

Here is my Code: 这是我的代码:

CREATE GLOBAL TEMPORARY TABLE NameRow (nom VARCHAR2(100), rowc INTEGER)
  on commit delete ROWS;
  insert into NameRow(nom, rowc) select table_name, num_rows from user_tables where temporary = 'N' and num_rows > 0;
    --select * from namerow;
    --select count(nom) from namerow;
    --drop table namerow;
    --no need for the row count > 0 because that was already done above
 declare
  counter number := 0;
  totalrecords number := 0;
  nmbre varchar2(100);
 BEGIN

    Select count(nom) into totalrecords from namerow;
    WHILE counter <= totalrecords LOOP
      select nom into nmbre from NameRow where rownum =1;
    SET SPOOL ON
    SPOOL c:\Users\l.r.enchaustegui\Documents\reporepo\||nmbre||.csv
    select /*csv*/ * from HR.nmbre;
    SET SPOOL OFF 
        delete from namerow where rownum=1;
        counter := counter + 1;
    End loop;
 END;

Code Explained: 代码说明:

  • A temporary table is created with a varchare and integer column to record all tables in DB with their RowCount 使用varchare和integer列创建一个临时表,以记录DB中具有RowCount的所有表
  • Table Names inserted in the temp table must be non temporary and have more than 0 row count 临时表中插入的表名必须是非临时的,并且行数超过0

Next segment 下一段

  • Declaring 3 variables: 2 integers and a varchar 声明3个变量:2个整数和一个varchar
  • 2 Integers: 1 is a counter for the loop. 2个整数:1是循环的计数器。 1 records the total rows in the Temp table which will serve as the max iterations in the loop. 1将记录在Temp表中的总行数用作循环中的最大迭代次数。
  • Varchar: Nmbre will record the name of the 1st table name within the temp table. Varchar:Nmbre将在临时表中记录第一个表的名称。

Next Segment 下一段

  • Spool into the following path, using the variable Nmbre to dynamically name the CSV file 后台处理到以下路径,使用变量Nmbre动态命名CSV文件
  • Spool Query using the variable Nmbre to dynamically select table 假脱机查询使用变量Nmbre动态选择表
  • Delete 1st row from temp table [serves to rotate into the next tablename] 从临时表中删除第一行[用于轮换到下一个表名]
  • Spool Off 假脱机
  • Loop
  • End loop; 结束循环; End; 结束;

Where am I wrong? 我哪里错了? Also, I am getting this error: 另外,我收到此错误:

Bonus Round: Constrained to SQL Developer 奖金回合:限于SQL Developer

Here's an option using SQLcl. 这是使用SQLcl的选项。 SQLcl is the guts of SQLDEV but wrappered into a cmd line. SQLcl是SQLDEV的胆量,但包装到cmd行中。 Also being java the scripting abilities of core java is available. 同样是Java,也可以使用核心Java的脚本编写能力。 This is using JavaScript as the scripting engine. 这使用JavaScript作为脚本引擎。

We have some doc and lots of example of how this all works out on github here: https://github.com/oracle/oracle-db-tools/tree/master/sqlcl 我们在github上有一些文档和许多示例说明了这些工作原理: https//github.com/oracle/oracle-db-tools/tree/master/sqlcl

script
 var binds = {};

// get complete list of tables
 var tables = util.executeReturnList("select table_name from user_tables", binds);

 for (i = 0; i < tables.length; i++) {
   // get count of rows
    var rows = util.executeReturnOneCol('select count(1)  from ' +  tables[i].TABLE_NAME );
    ctx.write( tables[i].TABLE_NAME + ">>"  + rows + " \n" ) ;

    // if more than zero dump to a csv file
    if ( rows > 0 ){
        sqlcl.setStmt("set sqlformat csv ")
        sqlcl.run();
        sqlcl.setStmt("spool " + tables[i].TABLE_NAME + ".csv")
        sqlcl.run();

        sqlcl.setStmt("select * from  " + tables[i].TABLE_NAME )
        sqlcl.run();
        sqlcl.setStmt("spool off")
        sqlcl.run();

    }
 }
/

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

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