问题
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!
回答1:
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;
回答2:
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
回答3:
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
回答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.
来源:https://stackoverflow.com/questions/38592148/oracle-export-select-statement-result-set-as-insert-sql-statements-similar-to