oracle 需要关于使用 PL/SQL 以动态方式将表数据输出到 CSV 的想法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/321455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need ideas on outputting table data to a CSV using PL/SQL in a dynamic fashion
提问by Robert
The task is to take a list of tables which is changeable.
任务是获取可更改的表列表。
Write a piece of PL/SQL that when executed outputs every tables rows into individual csv files.
编写一段 PL/SQL,执行时将每个表行输出到单独的 csv 文件中。
So if 5 tables. You will get 5 CSV files with the relevant table data in it.
所以如果有5张桌子。您将获得 5 个包含相关表格数据的 CSV 文件。
The CSV should be | delimited and have " around each value (for easy import to excel)
CSV 应该是 | 分隔并在每个值周围加上“(以便于导入到 excel)
All I know is the list of tables.
我所知道的是表格列表。
So load the list into an array at the top of the procedure, loop through this list and use UTL_FILE to output each row on a line by line basis.
因此将列表加载到过程顶部的数组中,循环遍历此列表并使用 UTL_FILE 逐行输出每一行。
I'm stuffed wondering if I need a cursor per table or if the cursor can be used dynamically to store the results from each table.
我很想知道每个表是否需要一个游标,或者是否可以动态使用游标来存储每个表的结果。
p.s. each file must also contain the column headings as the first row.
ps 每个文件还必须包含列标题作为第一行。
Is it even possible ? There is a list of over 30 tables, some of the tables have over 200 columns.
甚至有可能吗?有一个包含 30 多个表的列表,其中一些表有 200 多个列。
So ideas please :).
所以想法请:)。
I'm slowly thinking this isn't possible. as i need some dynamic SQL that can gather all the column names etc. I'm getting bogged down!
我慢慢地认为这是不可能的。因为我需要一些可以收集所有列名等的动态 SQL。我陷入了困境!
It can't be a SQL script and simply spooling the output. All we ever want to do is add or remove tables from the array declaration.
它不能是 SQL 脚本,不能只是对输出进行假脱机处理。我们想要做的就是从数组声明中添加或删除表。
采纳答案by Tony Andrews
回答by Tony Andrews
First Oracle stores all of that data in views that you have access to.
首先,Oracle 将所有这些数据存储在您有权访问的视图中。
SELECT * FROM ALL_TAB_COLUMNS
will get you a list of the columns for a table. That will make creating the column headers for the file simple.
将为您提供表的列列表。这将使为文件创建列标题变得简单。
The rest is just unloading data into a flat file. You can find recipes for that here.
其余的只是将数据卸载到平面文件中。你可以在这里找到食谱。
Here's a link directly to the code.
这是代码的直接链接。
回答by user34850
Ther are several options.
有几种选择。
- You can use UTL_FILE do dump preformatted text strings into files. Just make a cursor that outputs all data columns concatenated with delimiters.
- 您可以使用 UTL_FILE 将预先格式化的文本字符串转储到文件中。只需制作一个游标,输出所有用分隔符连接的数据列。
Something like this:
像这样的东西:
DECLARE
TYPE IDCurTyp IS REF CURSOR;
fo UTL_FILE.FILE_TYPE;
varRow VARCHAR2(4000);
cur_output IDCurTyp;
BEGIN
fo := UTL_FILE.FOPEN('BILLING_DIR','BillingFile1.csv', 'W', 2000)
OPEN cur_output FOR
'SELECT ''"'' || t1.col1 || ''",'' || t1.col2 || ''","'' || t1.col2 || ''"'' FROM t1'
LOOP
FETCH cur_output INTO varRow;
EXIT WHEN cur_output%NOTFOUND;
UTL_FILE.putf( fo, '%s\n', varRow );
END LOOP;
CLOSE cur_output;
UTL_FILE.FCLOSE( fo );
END:
- Instead of preformatted text create a package that accepts SQL query and then uses DBMS_SQL package to parse it and extract column names, etc and create text strings to dump using UTL_FILE once again.
- 不是预先格式化的文本,而是创建一个接受 SQL 查询的包,然后使用 DBMS_SQL 包来解析它并提取列名等,并再次使用 UTL_FILE 创建要转储的文本字符串。