6

I am looking for a solution to create the SQL INSERT statements using PL/SQL script for the select statement result set. Looking for similar feature available in the SQL Developer tool (export --> format insert) but I want the solution as script rather than using any tool(s).

I have referred the below solution. However, I would like to know whether any better way to do it as the solution is old and not very simple.

EXPORT AS INSERT STATEMENTS: But in SQL Plus the line overrides 2500 characters!

5 Answers 5

11

I just found a simple solution for my problem using oracle hint ("insert"). This automatically take care the data type as well. My table has only string and numeric data types, so it works fine for me. I have not tested the solution for other data types. However, I hope it would work for other data types as well.

set linesize 2000
set pagesize 10
spool "c:\myoutput.txt";
select /*insert*/ * from SAMPLE_TABLE;
spool off;
Sign up to request clarification or add additional context in comments.

2 Comments

SQL developer and SQLcl
I ran this in SQL developer as I had over 40000 select queries over 100 tables with different column set. Initially, I was using ctrl + enter which was wrong and F5 is the correct way to get it running.
8

as a script you say...

Use SQLcl - it's a command-line interface for SQL Developer.

You can create a .bat or .sh script to launch SQLcl, run your query, spool it to a file, no need to write any code.

set pagesize...
set head...
set feedback...
spool...

set sqlformat insert
select * from sample_table;
spool off
exit

uses the same code as SQL Developer to generate the INSERTs, only avail from a CLI/script

Comments

4
set sqlformat insert

In my case initially it didn't work in SQL developer as the version was version 3.2 and resulted with message in the script output:

oracle unknown set option sqlformat insert

As i upgraded to the SQL developer 4.2 and above and using "Run Script" (F5) i was able to export the select records as the insert scripts.

1 Comment

Thanks a ton Ram! I was executing it as a statement (Ctrl+Enter) instead of running it as a Script (F5)!
1

You can do it with an for loop and dbms output as you want, even as merge statements like Toad, just make the buffer big enough:

BEGIN
   FOR r_cur IN (  SELECT column_one,
                          column_two,
                          column_three,
                          column_value
                     FROM some_table
                    WHERE column_one LIKE 'something%'
                 ORDER BY column_one, column_two, column_three)
   LOOP
      DBMS_OUTPUT.put_line (
            'MERGE INTO some_table A USING
 (SELECT\n
  ''' || r_cur.column_one || ''' as column_one,
  ''' || r_cur.column_two || ''' as column_two,
  ''' || r_cur.column_three || ''' as column_three,
  ''' || r_cur.column_value || ''' as column_value
  FROM DUAL) B
ON (A.column_one = B.column_one and A.column_two = B.column_two and A.column_three = B.column_three)
WHEN NOT MATCHED THEN 
INSERT (
  column_one, column_two, column_three, column_value)
VALUES (
  B.column_one, B.column_two, B.column_three, B.column_value)
WHEN MATCHED THEN
UPDATE SET 
  A.column_value = B.column_value;
'         );
   END LOOP;
END;
/

Comments

0

Not really PL/SQL, but basic SQL can handle a request as simple as this:

set linesize 2000
set pagesize 0
spool c:\myoutput.txt
select 'INSERT INTO SAMPLE_TABLE VALUES ('''||
  C01||''','||C02||','||C03||','||C04||','''||C05||''','''||
  C06||''','''||C07||''','||C08||','||C09||','''||C10||''','''||
  C10||''','''||C11||''','''||C12||''','''||C13||''','''||C14||''','''||
  C15||''','''||C16||''');'
from sample_table;
spool off

Note: This is using the table example from the URL you referenced in your post. In the example in the URL, C02, C03, C04, C08, and C09 are all NUMBER, the rest are CHAR or VARCHAR2

5 Comments

Thanks! Actually, the script doesn't take data type into consideration.
I'm unclear on what you mean. You need it to take data type into consideration? Assuming the DDL is the same for this table in both the source & target, then data type shouldn't be an issue as all fields in the source copy of the table already meet the necessary constraints. I may be reading this entirely wrong, forgive me if that is so!
Example : If the string value in the column has spaces, the insert script wouldn't work if it is not enclosed with single quotes.
Right - good point -- you'd have to do it like this: with mytest as (select 'hello there' as myfield from dual) select 'insert into mytable values ('''|| myfield||''');' from mytest; I'll edit my original post to reflect character strings
Thanks for your help! I have just found a simple solution using oracle hint.

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.