Stored proc to create user table via file import


I need to create a user space table that is shared by every one with access to that schema. I have a csv file with three columns (all text) that I want to import into this table. I dont know if  I can import into a user table in a stored proc in Oracle. Please help with syntax.

Thank you
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Mark GeerlingsConnect With a Mentor Database AdministratorCommented:
You will need a "create table..." script more like this:

CREATE TABLE [your_TableName]
    column_a  varchar2(30)
   ,column_b  varchar2(30)
   ,column_c varchar2(30)
organization external
(type oracle_loader
default directory [logical_dir_name]
access parameters
(records delimited by newline
 fields terminated by ','
 (column_a char(30,
 column_b  char(30),
 column_c  char(30)
location ('[filename.txt]')

1. The [logical_dir_name] must be a valid logical directory in your database, so this must correspond to a physical directory on your server (or at least accessible by your database sever, like on a shared NFS directory for example)
2. The [filename.txt] value must be constant - you can't change this every day.  What you can do is copy a new file over the existing one to change the contents (but it must obviously have the same column structure).
3. I just guessed at a length of 30 bytes.  You will have to set that length to match your data.
4. If you use this "external table" option, you don't need a stored procedure to load it.  You just have to copy the new data file to the directory whenever you have new data to import.
Pawan KumarDatabase ExpertCommented:
Try ..

1. Create Table

    a INT
   ,b INT
   ,c INT

Open in new window

2. Right click on tableName --> Import Data -> Select CSV file --> Verify Data - > Create Script and you are done.
slightwv (䄆 Netminder) Commented:
Creating a table is easy.
Creating a global temporary table is easy.
Loading a CSV file into a table is easy.
Creating a stored procedure to do work is easy.

Is the file on the client machine or the same server as the database?

Exactly what you are wanting to do is confusing.  Please provide specifics on what you need to do.

>>2. Right click on tableName --> Import Data -> Select CSV file --> Verify Data - > Create Script and you are done.

Assuming they are using the same GUI as you...  Can you specify what tool that is for?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LuckyLucksAuthor Commented:
No GUI, need to create a table and insert into it csv entries in something more automated script..
slightwv (䄆 Netminder) Commented:
two options:
1-Create the table and use SQL*Loader to load the data
2-If the CSV file is on the database server create an External Table and it saves a couple of steps.
LuckyLucksAuthor Commented:
external table option is the declaration : organization external??

If not using external table, can we implement in a stored proc?
LuckyLucksAuthor Commented:
Mark GeerlingsDatabase AdministratorCommented:
external table option is the declaration: "organization external"?

"If not using external table, can we implement in a stored proc?"
Yes, that is another option to get data from a file into an Oracle table.  For that you need:
1. A logical directory on the database server (just like for an "external table").
2. A PL/SQL procedure that uses procedures and functions in the supplied PL/SQL package: "utl_file" to: open, read a line (usually in a loop), and close the file after reading to the end.  
3. This procedure can insert each record that it reads into your table.

A third option for getting data from a file into an Oracle table is to use SQL*Loader .
slightwv (䄆 Netminder) Commented:
I would not use PL/SQL to load a CSV into an Oracle table.  If you cannot use External Tables for some reason, I would use SQL*Loader to load the CSV into the Oracle table.

The downside is, the load process is done outside of the database.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.