oracle - 防止 Oracle SQL Developer 在导出时截断 CLOB

标签 oracle oracle-sqldeveloper export-to-csv

我想将包含大型 CLOB 的查询结果导出到 CSV 文件。但是,一旦在 CSV 字段中导出,CLOB 会在大约 4K 个字符后被截断(即它们会过早地以“...”结尾)。如何防止 Oracle SQL Developer 在导出时截断 CLOB?
enter image description here

最佳答案

您可以绕过 Oracle SQL Developer 进行导出,例如您可以使用 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
    '''
    # http://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 = oracle_db.get_cursor()
    sql_filename = 'your_query.sql' # Write your query there
    cursor = oracle_db.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

关于oracle - 防止 Oracle SQL Developer 在导出时截断 CLOB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31632983/

相关文章:

mysql - 不是单组组函数oracle sum 与子查询sum

java - 使用 oracle 序列和序列生成器创建字符串 Id

sql-server - SQL Server 的 SET NOEXEC 或 NOPARSE 的 Oracle 等效项是什么?

sql - Oracle 调试技术

postgresql - 无法使用 aws_s3.query_export_to_s3 函数将 AWS RDS Postgres 表导出到 S3 中的 CSV

sql - Oracle create table with foreign key 错误 - 标识符无效

sql - 数字乘以常数会得到负数

oracle - 定时命令已过时

c++ - 通过 C++ 在 csv 中插入值

json - 输出分号分隔的字符串