简体   繁体   English

使用sql loader将数据从CSV文件加载到oracle表

[英]Loading data from CSV file to oracle table using sql loader

i want to load the data from CSV file to oracle table with this below condition. 我想用以下条件将数据从CSV文件加载到oracle表。

if the table is empty then only the data should be loaded into table otherwise data should not be loaded. 如果表是空的,则只应将数据加载到表中,否则不应加载数据。

is this requirement possible with sql loader ? 使用sql loader是否可以满足此要求? or do i need to use any other ETL tool like informatica ? 或者我是否需要使用任何其他ETL工具,如informatica?

You can use the loading option: INSERT for achieving it. 您可以使用加载选项: INSERT来实现它。

It requires the table to be empty before loading. 它要求在加载之前将表清空。 SQL*Loader terminates with an error if the table contains rows. 如果表包含行,则SQL * Loader将终止并显示错误。

You can refer: Loading Data into Empty Tables 您可以参考:将数据加载到空表中

Cheers!! 干杯!!

Regardless of which OS environment you're working on, You may always use ORACLE_LOADER access driver using External tables . 无论您正在使用哪种操作系统环境,您都可以使用外部表来使用ORACLE_LOADER访问驱动程序。

From the Oracle docs Choosing External Tables Versus SQL*Loader 从Oracle文档中选择外部表与SQL * Loader

The record parsing of external tables and SQL*Loader is very similar, so normally there is not a major performance difference for the same record format. 外部表和SQL * Loader的记录解析非常相似,因此通常对于相同的记录格式没有主要的性能差异。

All you need to have is a read/write on a directory object. 您需要拥有的只是对目录对象的读/写。 It's good if you can create one( CREATE DIRECTORY YOUR_DIR AS '<directory_path>' ) or else ask other superusers to give appropriate grants( GRANT READ ON DIRECTORY YOUR_DIR TO yourschema ). 如果您可以创建一个( CREATE DIRECTORY YOUR_DIR AS '<directory_path>' )或者让其他超级用户给予适当的授权( GRANT READ ON DIRECTORY YOUR_DIR TO yourschema ),这是GRANT READ ON DIRECTORY YOUR_DIR TO yourschema

The External Table can be created dynamically using EXECUTE IMMEDIATE 可以使用EXECUTE IMMEDIATE动态创建外部表

DECLARE
     l_exists INT;
BEGIN
SELECT CASE
     WHEN EXISTS ( SELECT 1
                     FROM yourmaintable
                   WHERE ROWNUM = 1
                 ) THEN 1
     ELSE 0 END
into l_exists from dual;

  IF  l_exists : = 0 THEN --If the Table is empty


EXECUTE IMMEDIATE q'{ CREATE TABLE your_ext_table   -- Using the alternate 
                                                    -- quoting mechanism
   (col1         INT,
    col2         VARCHAR2(20)
    col3         DATE)
 ORGANIZATION EXTERNAL
   (TYPE ORACLE_LOADER    -- Use the ORACLE_LOADER driver to load your file.
    DEFAULT DIRECTORY YOUR_DIR
    ACCESS PARAMETERS
      (RECORDS DELIMITED BY NEWLINE
       FIELDS (col1      CHAR(2),
               col2      VARCHAR2(20),
               col3      VARCHAR2(20) date_format DATE mask "mm/dd/yyyy"
              )
      )
    LOCATION ('yourfile.csv')
   )}'
     ;

EXECUTE IMMEDIATE 'INSERT INTO yourmaintable (col1,col2,col3)  --dynamic Insert
                   select * FROM your_ext_table';
END IF;



END;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Oracle中使用SQL LOADER导入CSV文件 - Using SQL LOADER in Oracle to import CSV file 将动态 csv 文件(文件数据每天/每周更改)中的数据加载到 Oracle SQL 表中 - Loading data from a dynamic csv file ( file data changes daily/weekly) into a Oracle SQL table oracle sql loader在同一表的不同列中加载数据 - oracle sql loader loading data in different columns in same table SQL-从CSV文件加载数据并使用该数据在另一个表中查找实例 - SQL - Loading data from CSV file & using that data to find instances in another table 将 CSV 文件数据加载到 Oracle 中的表变量(索引表)中 - Loading CSV file data into a Table Variable (Index-by table) in Oracle 使用SQL Loader将csv文件导入到表中[但是没有。 [专栏] - import csv file into table using SQL Loader [but large no. of Columns] SQL loader:将具有嵌入式\\ r \\ n的.CSV文件加载到oracle中 - Sql loader: Loading .CSV files with embedded \r\n into oracle 使用 SQL 加载程序在 Oracle 中加载 XML 文件 - Load XML File in Oracle using SQL Loader oracle 10g示例中如何通过sql loader将一个数据文件上传到多个表中 - How to one data file upload into multiple table by sql loader in oracle 10g example 我正在尝试使用sql loader将数据加载到oracle表中 - I'm trying to load data into my oracle table using sql loader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM