Oracle : Export select statement result set as INSERT SQL Statements similar to SQL developer export

对着背影说爱祢 提交于 2019-12-01 06:53:23

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;

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!