57

The only thing I don't have an automated tool for when working with Oracle is a program that can create INSERT INTO scripts.

I don't desperately need it so I'm not going to spend money on it. I'm just wondering if there is anything out there that can be used to generate INSERT INTO scripts given an existing database without spending lots of money.

I've searched through Oracle with no luck in finding such a feature.

It exists in PL/SQL Developer, but errors for BLOB fields.

1
  • 1
    Can't you use SQL*Loader? (I know, it's a bit more effort to get set up, but at least you can create a "skin" for each table and then dump contents to your file) Commented Oct 30, 2009 at 11:10

10 Answers 10

53

Oracle's free SQL Developer will do this:

http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index.html

You just find your table, right-click on it and choose Export Data->Insert

This will give you a file with your insert statements. You can also export the data in SQL Loader format as well.

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

5 Comments

Similarly you could try running(with F5) the sexier version of the above in Sql Developer: SELECT /*insert*/ * FROM your_table;
@gerodim Turn your comment into an answer - this should be the accepted one!!!! And thanks!
Best solution if your moving data from an environment (eg. test oracle server) to another (eg. dev oracle server)
@gerodim dude. After 6 years, your answer still pretty relevant. Best answer for this question.
Is there any way to script this so I can generate inserts for a dozen tables on a regular basis?
32

You can do that in PL/SQL Developer v10.
1. Click on Table that you want to generate script for.
2. Click Export data.
3. Check if table is selected that you want to export data for.
4. Click on SQL inserts tab.
5. Add where clause if you don't need the whole table.
6. Select file where you will find your SQL script.
7. Click export.
enter image description here

1 Comment

Very good for small ad hoc requirements, but if you have a set of 20 tables that you need to generate regularly then this is a very manual process.
7

Use a SQL function (I'm the author):

Usage:

select fn_gen_inserts('select * from tablename', 'p_new_owner_name', 'p_new_table_name')
from dual;

where:

p_sql            – dynamic query which will be used to export metadata rows
p_new_owner_name – owner name which will be used for generated INSERT
p_new_table_name – table name which will be used for generated INSERT

p_sql in this sample is 'select * from tablename'

You can find original source code here:

Ashish Kumar's script generates individually usable insert statements instead of a SQL block, but supports fewer datatypes.

Comments

5

I have been searching for a solution for this and found it today. Here is how you can do it.

  1. Open Oracle SQL Developer Query Builder

  2. Run the query

  3. Right click on result set and export

    https://i.sstatic.net/lJp9P.png

1 Comment

While this works, it forces the query to be executed twice. The first time when showing the results on screen, then again when exporting it to the file. It is not bad for small tables, but terrible for large ones.
3

You might execute something like this in the database:

select "insert into targettable(field1, field2, ...) values(" || field1 || ", " || field2 || ... || ");"
from targettable;

Something more sophisticated is here.

1 Comment

I came looking for this, sometimes it is easier to generate sql like this.
3

User @gerodim commented this on another answer but it deserves to be an answer of its own.

The following magic comment when run with F5 in SQLDeveloper will write out INSERT statements to the console output:

select /*insert*/ * from your_table;

So you could have a series of these statements if you want to regularly generate a script that populates a set of data, for exmaple a standard test data set.

select /*insert*/ * from employees;
select /*insert*/ * from roles;
select /*insert*/ * from offices;
select /*insert*/ * from it_equipment;

Unfortunately it seems to stop after 5,000 rows. Anyone know a solution to that? Some config maybe?

1 Comment

The 5000 limit is the default and can be changed from Tools->Preferences->Database->Worksheet. You can also do set sqlformat insert instead of modifying your query with the hint.
2

If you have an empty table the Export method won't work. As a workaround. I used the Table View of Oracle SQL Developer. and clicked on Columns. Sorted by Nullable so NO was on top. And then selected these non nullable values using shift + select for the range.

This allowed me to do one base insert. So that Export could prepare a proper all columns insert.

Comments

1

This is a workaround for SQL Developer when table is empty (no rows) and you need just a boiler plate for an insert script of the table.

  1. Click right button in table (table node in left pane named Connections)
  2. From context menu choose Table -> Generate Table API...
  3. New tab will open with CRUD scripts (roughly speaking)
  4. Find piece of script with 'insert' and copy it

Comments

0

If you have to load a lot of data into tables on a regular basis, check out SQL Loader or external tables. Should be much faster than individual Inserts.

Comments

-2

You can also use MyGeneration (free tool) to write your own sql generated scripts. There is a "insert into" script for SQL Server included in MyGeneration, which can be easily changed to run under Oracle.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.