使用 Oracle SQL Developer 将 CLOB 导出到文本文件

发布于 2024-08-05 08:50:36 字数 258 浏览 13 评论 0原文

我正在使用 Oracle SQL Developer 并尝试将表导出到 CSV 文件。有些字段是 CLOB 字段,在许多情况下,导出时条目会被截断。我正在寻找一种方法来解决整个问题,因为我的最终目标是不在这里使用 Oracle(我收到了一个 Oracle 转储 - 它已加载到 Oracle 数据库中,但我正在使用另一种格式的数据,因此通过CSV 作为中介)。

如果对此有多种解决方案,鉴于这对我来说是一次性过程,我不介意使用更多黑客类型的解决方案来解决更多涉及的“正确执行”解决方案。

I am using Oracle SQL Developer and trying to export a table to a CSV file. Some of the fields are CLOB fields, and in many cases the entries are truncated when the export happens. I'm looking for a way to get the whole thing out, as my end goal is to not use Oracle here (I received an Oracle dump - which was loaded into an oracle db, but am using the data in another format so going via CSV as an intermediary).

If there are multiple solutions to this, given that it is a one time procedure for me, I don't mind the more hack-ish type solutions to more involved "do it right" solutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦在夏天 2024-08-12 08:50:36

如果您有权访问数据库盒上的文件系统,您可以执行以下操作:

CREATE OR REPLACE DIRECTORY documents AS 'C:\';
SET SERVEROUTPUT ON
DECLARE
  l_file    UTL_FILE.FILE_TYPE;
  l_clob    CLOB;
  l_buffer  VARCHAR2(32767);
  l_amount  BINARY_INTEGER := 32767;
  l_pos     INTEGER := 1;
BEGIN
  SELECT col1
  INTO   l_clob
  FROM   tab1
  WHERE  rownum = 1;

  l_file := UTL_FILE.fopen('DOCUMENTS', 'Sample2.txt', 'w', 32767);

  LOOP
    DBMS_LOB.read (l_clob, l_amount, l_pos, l_buffer);
    UTL_FILE.put(l_file, l_buffer);
    l_pos := l_pos + l_amount;
  END LOOP;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.put_line(SQLERRM);
    UTL_FILE.fclose(l_file);
END;
/

我复制并粘贴了 来自此网站

您还可能会找到有关 UTL_FILE 的上一个问题 有用。它解决了导出到 CSV 的问题。然而,我不知道或没有 UTL_FILE 如何处理 CLOB 的经验。

if you have access to the file system on your database box you could do something like this:

CREATE OR REPLACE DIRECTORY documents AS 'C:\';
SET SERVEROUTPUT ON
DECLARE
  l_file    UTL_FILE.FILE_TYPE;
  l_clob    CLOB;
  l_buffer  VARCHAR2(32767);
  l_amount  BINARY_INTEGER := 32767;
  l_pos     INTEGER := 1;
BEGIN
  SELECT col1
  INTO   l_clob
  FROM   tab1
  WHERE  rownum = 1;

  l_file := UTL_FILE.fopen('DOCUMENTS', 'Sample2.txt', 'w', 32767);

  LOOP
    DBMS_LOB.read (l_clob, l_amount, l_pos, l_buffer);
    UTL_FILE.put(l_file, l_buffer);
    l_pos := l_pos + l_amount;
  END LOOP;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.put_line(SQLERRM);
    UTL_FILE.fclose(l_file);
END;
/

Which I copied and pasted from this site.

You may also find this previous question about UTL_FILE useful. It addresses exporting to CSV. I have no idea or experience with how UTL_FILE handles CLOBs, however.

腹黑女流氓 2024-08-12 08:50:36

您可以使用 Python 脚本来处理导出,CLOB 不会被截断:

from __future__ import print_function
from __future__ import division

import time
import cx_Oracle

def get_cursor():
    '''
    Get a cursor to the database
    '''
    # https://stackoverflow.com/questions/24149138/cx-oracle-doesnt-connect-when-using-sid-instead-of-service-name-on-connection-s
    # http://www.oracle.com/technetwork/articles/dsl/prez-python-queries-101587.html
    ip = '' # E.g. '127.0.0.1'
    port = '' # e.g. '3306'
    sid = ''
    dsnStr = cx_Oracle.makedsn(ip, port, sid)
    username = '' # E.g. 'FRANCK'
    password = '' # E.g. '123456'
    db = cx_Oracle.connect(user=username, password=password, dsn=dsnStr)    
    cursor = db.cursor()
    return cursor

def read_sql(filename):
    '''
    Read an SQL file and return it as a string
    '''
    file = open(filename, 'r')
    return ' '.join(file.readlines()).replace(';', '')

def execute_sql_file(filename, cursor, verbose = False, display_query = False):
    '''
    Execute an SQL file and return the results
    '''
    sql = read_sql(filename)
    if display_query: print(sql)
    start = time.time()
    if verbose: print('SQL query started... ', end='')
    cursor.execute(sql)
    if verbose: 
        end = time.time()
        print('SQL query done. (took {0} seconds)'.format(end - start))
    return cursor


def main():
    '''
    This is the main function
    '''
    # Demo:
    cursor = get_cursor()
    sql_filename = 'your_query.sql' # Write your query there
    cursor = execute_sql_file(sql_filename, cursor, True)    
    result_filename = 'result.csv'   # Will export your query result there
    result_file = open(result_filename, 'w')
    delimiter = ','    
    for row in cursor:
        for count, column in enumerate(row):
            if count > 0: result_file.write(delimiter)
            result_file.write(str(column))
        result_file.write('\n')
    result_file.close()


if __name__ == "__main__":
    main()
    #cProfile.run('main()') # if you want to do some profiling

仅供参考:帮助安装 cx_Oracle

You can use a Python script to take care of the export, the CLOBs won't get truncated:

from __future__ import print_function
from __future__ import division

import time
import cx_Oracle

def get_cursor():
    '''
    Get a cursor to the database
    '''
    # https://stackoverflow.com/questions/24149138/cx-oracle-doesnt-connect-when-using-sid-instead-of-service-name-on-connection-s
    # http://www.oracle.com/technetwork/articles/dsl/prez-python-queries-101587.html
    ip = '' # E.g. '127.0.0.1'
    port = '' # e.g. '3306'
    sid = ''
    dsnStr = cx_Oracle.makedsn(ip, port, sid)
    username = '' # E.g. 'FRANCK'
    password = '' # E.g. '123456'
    db = cx_Oracle.connect(user=username, password=password, dsn=dsnStr)    
    cursor = db.cursor()
    return cursor

def read_sql(filename):
    '''
    Read an SQL file and return it as a string
    '''
    file = open(filename, 'r')
    return ' '.join(file.readlines()).replace(';', '')

def execute_sql_file(filename, cursor, verbose = False, display_query = False):
    '''
    Execute an SQL file and return the results
    '''
    sql = read_sql(filename)
    if display_query: print(sql)
    start = time.time()
    if verbose: print('SQL query started... ', end='')
    cursor.execute(sql)
    if verbose: 
        end = time.time()
        print('SQL query done. (took {0} seconds)'.format(end - start))
    return cursor


def main():
    '''
    This is the main function
    '''
    # Demo:
    cursor = get_cursor()
    sql_filename = 'your_query.sql' # Write your query there
    cursor = execute_sql_file(sql_filename, cursor, True)    
    result_filename = 'result.csv'   # Will export your query result there
    result_file = open(result_filename, 'w')
    delimiter = ','    
    for row in cursor:
        for count, column in enumerate(row):
            if count > 0: result_file.write(delimiter)
            result_file.write(str(column))
        result_file.write('\n')
    result_file.close()


if __name__ == "__main__":
    main()
    #cProfile.run('main()') # if you want to do some profiling

FYI: Help installing cx_Oracle

满身野味 2024-08-12 08:50:36

假设 Oracle 转储是指 .dmp(来自导出或 expdp),那么您正在查看一个二进制文件。您需要将转储文件导入 Oracle 数据库,然后使用 UTL_FILE 或其他方式将数据导出为纯文本。

assuming by an Oracle dump you meant a .dmp (either from export or expdp), you're looking at a binary file. You'll need to import the dumpfile into an Oracle database and then export the data to plain text using UTL_FILE or other means.

夜巴黎 2024-08-12 08:50:36

这是一个简短而通用的 python 脚本,它就是这样做的 - 将表(其中包含 CLOB 字段)转储到平面 csv 文件: OraDump

Here is a short yet general python script that does just this - dumping tables (with CLOB fields, among the rest) to a flat csv file: OraDump

無心 2024-08-12 08:50:36

由于明确提到了 Oracle SQL Developer 并鼓励“hack-ish 类型解决方案”(以防万一有人仍然需要……):

如果从 SQL 工作表(在 Oracle SQL Developer 中)执行单个语句,则会显示结果作为一张桌子。
单击结果表后,使用键盘快捷键或编辑菜单首先标记整个表,然后复制其内容。
继续使用您选择的文本编辑器。粘贴。保存到文件。希望完成。 ;-)

即使对于超过 4000 个字符的 CLOB 也适用。它是否真的有帮助,很大程度上取决于 CLOB 的实际内容。有时,一些 SQL 预处理可能会帮助您...

或者尝试在结果表的本地菜单中导出...。从那里开始使用 Excel 95-2003 格式选项可能会起作用(比 CSV 或文本/TSV 更好)。

如果需要调整/进一步的细节,请发表评论。

As Oracle SQL Developer is explicitly mentioned and "hack-ish type solutions" were encouraged (and in case somebody is still in need…):

If a single statement is executed from a SQL Worksheet (in Oracle SQL Developer), the result is displayed as a table.
After clicking the result table, use either keyboard shortcuts or the Edit menu to first mark the whole table and then to copy its content.
Proceed to your text editor of choice. Paste. Save to a file. Hopefully done. ;-)

Works even for CLOBs exceeding 4000 characters. Whether or not it actually helps, will very much depend on the CLOBs' actual content. Sometimes some SQL pre-processing might get you there…

Alternatively try in the result table's local menu Export…. Going from there through the Excel 95-2003 format option might work (better than CSV or text/TSV).

Please comment, if and as this requires adjustment / further detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文