How to insert data directly from Excel to Oracle Database

守給你的承諾、 提交于 2019-12-06 13:27:30

问题


I would like to know how to insert data from Excel to Oracle meaning lets say i have a worksheet full of data and i want to insert them all into Oracle database.

how do i do that?

thanks in advance.


回答1:


Try creating Insert query in Excel(if data is not that huge):
like :
in AA1 cell write : 'INSERT INTO <TABLE> VALUES('
then respective values from different cells(i.e. 'A1,'+'B1,'...+')')
it will create Insert scripts in excel. Just copy the scripts and execute.

these are sample queries, plz write your own queries according to your requirement for INSERT STATEMENT.




回答2:


Many Oracle IDEs offer this feature.

Oracle SQL Developer:

  • imports from CSV to a new or existing table
  • imports from XLS/XLSX files to a new or existing table
  • can setup an External Table or SQL*Loader session to import from a delimited file to a new or existing table
  • offers a command-line interface for automation purposes

I talk about these features here.




回答3:


convert your excel file format to .csv format with using save as option

Save below content in a file with .ctl extension in the D drive under folder_name folder. Before running this .ctl make sure that table should be present in your schema with the distinct column names not like in your posted image. And the column name should be match with names of .ctl file(sc_id, sal, etc). And the datatypes of your columns should be match with the data present in a .csv file. And also make sure that your table should be empty otherwise you should use truncate or append options in your .ctl file.

 LOAD DATA
    INFILE 'D:\folder_name\csvfile_name.CSV'
    BADFILE 'D:\folder_name\csvfile_name.BAD'
    DISCARDFILE 'D:\folder_name\csvfile_name.log'
    logfile 'D:\folder_name\csvfile_name.DSC'
    iNTO TABLE schema_name.table_name
    FIELDS TERMINATED BY ','
    TRAILING NULLCOLS

    (
    sc_id,
    sal,
    sc_cd1,
    coll,
    sc_cd2,
    bill_mas,
   sc_cd3,
   wk_sal,
   check_bill_month,
   check_sale_wk
    ) 

Run your .ctl file in sql plus with use of below commands

sqlldr schema_name/password@databasename control=your control file path.

If any error occurs while loading data into table those will logged into .log file. For learn more about sqlloader Refer http://docs.oracle.com/cd/B10501_01/server.920/a96652/ch05.htm




回答4:


You can use sql loader, but if you want a more robust solution (especially if the data will be modified directly in the spreadsheet) then look into external tables in Oracle. You can build a database table directly from the spreadsheet, the code looks like this:

CREATE TABLE YOUR_SCHEMA.YOUR_TABLE
(
  ACCT_NO                   VARCHAR2(15 BYTE),
  PRODUCT_TYPE              VARCHAR2(50 BYTE),
  ORGINATION_DATE           VARCHAR2(20 BYTE),
  MATURITY_DATE             VARCHAR2(20 BYTE),
  FIXED_PERIOD_START_DATE   VARCHAR2(30 BYTE),
  FIXED_PERIOD_END_DATE     VARCHAR2(30 BYTE),
  ORIGINAL_LOAN_AMOUNT      NUMBER,
  CURRENT_BALANCE           NUMBER
)
ORGANIZATION EXTERNAL
  (  TYPE ORACLE_LOADER
     DEFAULT DIRECTORY YOUR_DIRECTORY
     ACCESS PARAMETERS 
       ( RECORDS DELIMITED  BY NEWLINE
         LOGFILE YOUR_DIRECTORY:'your_log.log'
         BADFILE YOUR_DIRECTORY:'your_bad.bad'
         FIELDS TERMINATED BY ','
         MISSING FIELD VALUES ARE NULL )
     LOCATION (YOUR_DIRECTORY:'your_file.csv')
  )

Note that anything with the word "your" in it above must be changed to whatever you want to name your files and directories. Also note that "YOUR_DIRECTORY" is an actual database object you must create:

CREATE OR REPLACE DIRECTORY YOUR_DIRECTORY AS 'C:\workspaces\test\XL2ExternalTables'



回答5:


Please refer for the answer. https://community.oracle.com/thread/2166713?start=0

I am just copying the approach what worked for me from above link

In Oracle sql developer go to Tables --> select import data ---> select your excel or csv file --> it will display the column --> import the data into a table.




回答6:


I have a query, I already have a table ABC and I deleted few rows from it, but before that I took export of rows I deleted in excel sheet. Now I need to insert those records back into the table. Since record count is more, I cannot write Insert statement for all. So is there any way, by which I can import the data back into table using excel sheet. Thanks, Abhishek



来源:https://stackoverflow.com/questions/32117050/how-to-insert-data-directly-from-excel-to-oracle-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!