Reputation: 4754
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.
Upvotes: 13
Views: 70077
Reputation: 2393
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.
Upvotes: 0
Reputation: 83187
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
Upvotes: 2
Reputation: 1558
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
Upvotes: 0
Reputation: 60746
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.
Upvotes: 17
Reputation: 2956
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.
Upvotes: 0