Home > Database > Mysql Tutorial > How to Export Oracle Database Queries to CSV Using SQLPLUS?

How to Export Oracle Database Queries to CSV Using SQLPLUS?

Barbara Streisand
Release: 2025-01-17 20:36:11
Original
878 people have browsed it

How to Export Oracle Database Queries to CSV Using SQLPLUS?

*Exporting Oracle Database Data to CSV using SQLPlus**

SQL*Plus offers a straightforward method for exporting data from Oracle databases to CSV files, eliminating the need for complex tools. This guide details how to effectively spool your queries to a CSV.

To generate a CSV file, configure the following SQL*Plus settings:

<code class="language-sql">SET COLSEP ','     -- Use comma as column separator
SET PAGESIZE 0   -- Suppress header rows
SET TRIMSPOOL ON -- Remove trailing spaces
SET HEADSEP OFF  -- Optional; may improve heading formatting
SET LINESIZE X   -- X represents the total width of all columns
SET NUMW X       -- X defines the desired width for numeric fields (prevents scientific notation)</code>
Copy after login

Next, create your SQL query and spool the results to a CSV file:

<code class="language-sql">SPOOL myfile.csv

SELECT table_name, tablespace_name
FROM all_tables
WHERE owner = 'SYS'
  AND tablespace_name IS NOT NULL;

SPOOL OFF</code>
Copy after login

The resulting myfile.csv will contain comma-separated values, free of extra whitespace.

For a more streamlined approach, consider using sed to remove any remaining whitespace before commas:

<code class="language-bash">sed 's/\s+,/,/' myfile.csv > myfile_cleaned.csv</code>
Copy after login

This command cleans the CSV, ensuring a consistent and readily importable format. The output is written to myfile_cleaned.csv.

The above is the detailed content of How to Export Oracle Database Queries to CSV Using SQLPLUS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template