Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Cara Mengekspor Hasil Kueri ke File CSV di SQLcl (Oracle)

Jika Anda menggunakan SQLcl untuk menjalankan kueri terhadap Oracle Database, maka Anda dapat menggunakan SPOOL perintah untuk mengekspor hasil kueri Anda ke file CSV.

Contoh

Berikut ini contoh yang mengekspor seluruh tabel:

SET SQLFORMAT csv
SPOOL '/Users/barney/data/countries.csv';
SELECT * FROM countries;
SPOOL off;
SET SQLFORMAT ansiconsole 

Inilah yang dilakukannya, baris demi baris:

  1. Baris pertama saya menyetel SQLFORMAT ke csv . Ini memastikan bahwa output sebenarnya dipisahkan koma. Jika saya tidak melakukan ini, saya bisa mendapatkan file dengan .csv ekstensi, tetapi dengan konten yang tidak dipisahkan koma.
  2. Baris kedua adalah tempat kita menggunakan SPOOL perintah untuk menentukan di mana file output akan ditulis. Pastikan untuk mengubah /Users/barney/data/countries.csv bagian ke lokasi di sistem Anda.
  3. Pada baris ketiga, saya menjalankan kueri SQL – hasil yang saya ekspor. Dalam hal ini, saya mengekspor seluruh countries meja.
  4. Selanjutnya saya putar SPOOL mati.
  5. Akhirnya, saya selesai dengan mengatur SQLFORMAT kembali ke ansiconsole (yang saya gunakan sebelum menyetelnya ke csv ).

Berikut tampilan file yang dihasilkan:

"COUNTRY_ID","COUNTRY_NAME","REGION_ID""AR","Argentina",2"AU","Australia",3"BE","Belgia",1"BR",,"Brasil",2 "CA","Kanada",2"CH",,"Swiss",1"CN",,"China",3"DE",,"Jerman",1"DK",,"Denmark",1"EG",," Mesir",4"FR",,"Prancis",1"HK",,"HongKong",3"IL",,"Israel",4"IN",,"India",3"IT",,"Italia",1" JP", "Jepang",3"KW",,"Kuwait",4"MX",,"Meksiko",2"NG",,"Nigeria",4"NL",,"Belanda",1"SG",,"Singapura ",3"UK",,"Britania Raya",1"AS",,"Amerika Serikat",2"ZM",,"Zambia",4"ZW",,"Zimbabwe",425 baris dipilih. 

Hapus Tajuk Kolom

Anda dapat menghapus tajuk kolom dengan SET HEADING off :

SET SQLFORMAT csv
SET HEADING off
SPOOL '/Users/barney/data/countries.csv';
SELECT * FROM countries;
SPOOL off;
SET SQLFORMAT ansiconsole
SET HEADING on 

Hasil:

"AR",,"Argentina",2"AU",,"Australia",3"BE",,"Belgia",1"BR",,"Brasil",2"CA",,"Kanada",2"CH ","Swiss",1"CN",,"China",3"DE",,"Jerman",1"DK",,"Denmark",1"EG",,"Mesir",4"FR",,"Prancis" ,1"HK",,"HongKong",3"IL",,"Israel",4"IN",,"India",3"IT",,"Italia",1"JP",,"Jepang",3"KW" ,"Kuwait",4"MX",,"Meksiko",2"NG",,"Nigeria",4"NL",,"Belanda",1"SG",,"Singapura",3"UK",,"Inggris Raya" ,1"AS",,"Amerika Serikat",2"ZM",,"Zambia",4"ZW",,"Zimbabwe",425 baris dipilih. 

Dalam hal ini, saya mengubah HEADINGS hidupkan kembali setelah saya mengekspor file.

Hapus Masukan

Anda dapat menghapus X rows selected dengan SET FEEDBACK off :

SET SQLFORMAT csv
SET HEADING off
SET FEEDBACK off
SPOOL '/Users/barney/data/countries.csv';
SELECT * FROM countries;
SPOOL off;
SET SQLFORMAT ansiconsole
SET HEADING on 
SET FEEDBACK on 

Hasil:

"AR",,"Argentina",2"AU",,"Australia",3"BE",,"Belgia",1"BR",,"Brasil",2"CA",,"Kanada",2"CH ","Swiss",1"CN",,"China",3"DE",,"Jerman",1"DK",,"Denmark",1"EG",,"Mesir",4"FR",,"Prancis" ,1"HK",,"HongKong",3"IL",,"Israel",4"IN",,"India",3"IT",,"Italia",1"JP",,"Jepang",3"KW" ,"Kuwait",4"MX",,"Meksiko",2"NG",,"Nigeria",4"NL",,"Belanda",1"SG",,"Singapura",3"UK",,"Inggris Raya" ,1"US",,"Amerika Serikat",2"ZM",,"Zambia",4"ZW",,"Zimbabwe",4

Di sini, saya mengubah FEEDBACK hidupkan kembali setelah mengekspor file.

Beberapa Tabel

Dalam contoh ini, saya mengekspor hasil kueri yang sedikit lebih kompleks yang menggabungkan dua tabel:

SET SQLFORMAT csv 
SET HEADING off 
SET FEEDBACK off
SPOOL '/Users/barney/data/employees_jobs.csv';
SELECT 
    e.employee_id,
    e.first_name,
    e.last_name,
    e.salary,
    j.job_title
FROM employees e
INNER JOIN jobs j
ON e.job_id = j.job_id
WHERE e.salary BETWEEN 12000 AND 15000
ORDER BY SALARY DESC;
SPOOL off;
SET SQLFORMAT ansiconsole
SET HEADING on 
SET FEEDBACK on 

File yang dihasilkan:

145,"John",,"Russell",14000,"Manajer Penjualan"146,"Karen",,"Mitra",13500,"Manajer Penjualan"201,"Michael",,"Hartstein",13000,"Manajer Pemasaran "147,"Alberto",,"Errazuriz",12000,"Manajer Penjualan"205,"Shelley",,"Higgins",12000,"Manajer Akuntansi"108,"Nancy",,"Greenberg",12000,"Manajer Keuangan" 

Tambahkan Hasil

Secara default, SPOOL menggunakan REPLACE , yang menggantikan file jika sudah ada.

Namun, kita dapat menggunakan APPEND argumen untuk menambahkan hasil ke file.

SET SQLFORMAT csv 
SET HEADING off 
SET FEEDBACK off
SPOOL '/Users/barney/data/employees_jobs.csv' APPEND;
SELECT 
    e.employee_id,
    e.first_name,
    e.last_name,
    e.salary,
    j.job_title
FROM employees e
INNER JOIN jobs j
ON e.job_id = j.job_id
WHERE e.salary BETWEEN 11000 AND 11999
ORDER BY SALARY DESC;
SPOOL off;
SET SQLFORMAT ansiconsole
SET HEADING on 
SET FEEDBACK on 

File yang dihasilkan:

145,"John",,"Russell",14000,"Manajer Penjualan"146,"Karen",,"Mitra",13500,"Manajer Penjualan"201,"Michael",,"Hartstein",13000,"Manajer Pemasaran "147,"Alberto",,"Errazuriz",12000,"Manajer Penjualan"205,"Shelley",,"Higgins",12000,"Manajer Akuntansi"108,"Nancy",,"Greenberg",12000,"Manajer Keuangan"168 ,"Lisa",,"Ozer",11500,"Perwakilan Penjualan"174,"Ellen",,"Abel",11000,"Perwakilan Penjualan"114,"Den",,"Raphaely",11000,"Manajer Pembelian"148," Gerald","Cambrault",11000,"Manajer Penjualan"

Contoh ini menambahkan hasil ke file yang dibuat (dan diisi) pada contoh sebelumnya.


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Siaga Buruk

  2. Oracle Database Explorer:Pelatihan dan Akreditasi Gratis

  3. Transformasi Baris ke Kolom Oracle

  4. Fungsi COS() di Oracle

  5. ORA-01264:Tidak dapat membuat nama file logfile