[英]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.