How to Import CSV in PL/SQL?
Importing CSV data into PL/SQL can be achieved through the following steps:
- Create an external table that matches the structure of a CSV file. External tables allow access and querying of external data files as if they were tables.
CREATE TABLE ext_table
(
column1 datatype,
column2 datatype,
...
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY directory_name
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY comma
...
)
LOCATION ('file_name.csv')
)
REJECT LIMIT UNLIMITED;
- Create a data loading script that imports data from an external table into the target table using the INSERT INTO statement.
INSERT INTO target_table
SELECT *
FROM ext_table;
- Run the data loading script to import the data from the CSV file into the target table in the database.
Notice:
- Make sure the structure of the external table matches the structure of the CSV file, otherwise the data import may fail.
- When creating an external table, it is necessary to specify parameters such as the delimiter and field format of the CSV file.
- Data in external tables can be queried through SQL statements, but cannot be directly updated or deleted in the external tables.
- When importing data, it is important to ensure that the paths, file names, and target table names of the CSV file are correct.