[英]Creating a CSV file per Loop | PLSQL Oracle SQL Developer
更新
鄉親們!
TL; DR
需要工作代碼來遍歷我所有表的現有列表,這些列表將創建CSV文件,並選擇每個表的前100行。 設置了一些變量來捕獲表名,這些變量將用於動態地調用表,進行選擇並命名文件。 必須通過PLSQL和SQLDeveloper完成。 你知道嗎?
情況如下:
問題:
這是我的代碼:
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;
代碼說明:
下一段
下一段
我哪里錯了? 另外,我收到此錯誤:
獎金回合:限於SQL Developer
這是使用SQLcl的選項。 SQLcl是SQLDEV的膽量,但包裝到cmd行中。 同樣是Java,也可以使用核心Java的腳本編寫能力。 這使用JavaScript作為腳本引擎。
我們在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.