[英]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: 情况如下:
Problems: 问题:
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: 代码说明:
Next segment 下一段
Next Segment 下一段
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.