1

I really need your help I am always work on SQL server, but now I am working on something else and that why I need your help. I m working on (Oracle SQL plus), I have a text file lets say the name test.txt and just I want to upload data from this file to database table using SQL plus

lets say the text file data:

001,mike,1-1-2018

002,jon,20-12-2017

003,bill 25-5-2018

how to write a code pl/sql on sql plus to upload the data from the text file to the table on my data base?? usually on SQL server I use Bulk insert, here what the methods?

I tried many from the internet but not solved.

Please help me Thanks a lot

1
  • You might want to look at SQL*Loader rather than trying to create your own solution, assuming the text file is on your client machine and not on the database server (if they are different). If the file is on the server you could look at external tables instead. A PL/SQL solution would need the file to be on the DB server too. Commented Aug 1, 2018 at 17:10

1 Answer 1

3

If the text file is on the same machine you're running SQL*Plus from, you can use the SQL*Loader utility.

As a simple example, lets say your table is:

create table your_table (id number, name varchar2(10), some_date date);

And you have a text file data.txt containing what you showed, but with a comma added on the third line:

001,mike,1-1-2018
002,jon,20-12-2017
003,bill,25-5-2018

You can create a basic SQL*Loader control file in the same directory, called say your_table.ctl, with something like:

LOAD DATA
INFILE 'data.txt'
APPEND
INTO TABLE your_table
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
  ID,
  NAME,
  SOME_DATE DATE "DD-MM-YYYY"
)

Look at the documentation to see what all those mean, particularly what APPEND means; you may want to TRUNCATE instead - but be careful with that.

Then run SQL*Loader from the command line (not from within SQL*Plus), using the same credentials and connect string you normally use to connect to the database:

sqlldr userid=usr/pwd@tns control=your_table.ctl

Once that has completed - assuming there are no errors reported on console ro in the log file it creates - then querying your table will show:

select * from your_table;

        ID NAME       SOME_DATE 
---------- ---------- ----------
         1 mike       2018-01-01
         2 jon        2017-12-20
         3 bill       2018-05-25

There are lots of other options and capabilities, but that might cover what you need at the moment.

Sign up to request clarification or add additional context in comments.

3 Comments

if txt file has: a,b,c and table column are like 1,b,3,4,a,c,7. how do i assign the txt values accordingly using plsql/bash script?
Please ask a new question explaining what you are trying to do and what issue you are having.
sharing it here, thank you. stackoverflow.com/questions/70033269/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.