Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Steven.

Asked: July 06, 2016 - 5:16 pm UTC

Last updated: March 09, 2018 - 2:32 am UTC

Version: Version 4.1.3.20

Viewed 10K+ times! This question is

You Asked

Hi Tom,


I am interested in using the SQL formatting option - /*insert*/ to create an insert script.
Your example of a simple insert script - where one pulls from a single table and creates
an insertion script of that table - bypassing having to use the sql developer export utility
is very hopeful. The results of my query - which is pulling on various tables - by default is
generating a target table = "MYTABLE". Naturally, I would expect to somewhere define the Target
table name - but I do not know how to do this... The resulting output fields are a mix - calling different
tables and some adhoc (dates, and other fields).

Example:

spool "C:\test.sql";

select /*insert*/
ZipCluster.SchemeID as CLUSTER_SCHEME_ID,
ZipCluster.ZIPCODE + 100000 AS ZIPCODE_ID,
ZipCluster.NumOfDay AS DAY_OF_WEEK_ID,
ZipCluster.Cluster as CLUSTER_NAME,
ZipCluster.ClusterLevel AS CLUSTER_LEVEL_ID,
CREATED_USER_ID = 1,
CREATED_DATETIME = CONVERT(VARCHAR(19),GETDATE(),120),
LASTMODIFIED_USER_ID = 1,
LASTMODIFIED_DATETIME = CONVERT(VARCHAR(19),GETDATE(),120)
from ZipCluster, ClusterScheme WHERE
ClusterScheme.Publication_ID = 1 and
ClusterScheme.SchemeID = 10 and
ZipCluster.SchemeID = ClusterScheme.SchemeID


Script Output screen (only shown one as example):

Insert into MYTABLE (CLUSTER_SCHEME_ID,ZIPCODE_ID,DAY_OF_WEEK_ID,CLUSTER_NAME,CLUSTER_LEVEL_ID,CREATED_USER_ID,CREATED_DATETIME,LASTMODIFIED_USER_ID,LASTMODIFIED_DATETIME) values (10,102140,'7','08',1,1,'2016-07-06 13:10:06',1,'2016-07-06 13:10:06');


Thank-you !
Steve G.

and Connor said...

Interestingly my results differ - it appears to be the first name found after a "from".

eg

select /*insert*/ e.empno, d.dname 
  from scott.dept d,
       scott.emp e
  where e.deptno = d.deptno   


gives me scott.dept as the table, but if I do

with xxx as
( select e.empno, d.dname 
  from scott.dept d,
       scott.emp e
  where e.deptno = d.deptno     
)
select /*insert*/ * from xxx


I *still* get scott.dept as the target table.

But the "insert" facility really was designed to give you a "helping hand" rather than be a definitive generation tool. Hence a simple "change-all" command on the script, either manually or via sed (for example) would do the trick

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

/*insert*/ auto generating result table name as "MYTABLE"

Steven Gonzalez, July 07, 2016 - 11:51 am UTC

Hey Connor,

A few things to note:
1. I believe I am getting the "MYTABLE" table name in
my query (and you are not) because in the return values to
my query I have adhoc/hardcoded fields such as Date and a
userID # (not values returned from a table). When I used
a simpler query - without these values - I also got back
the first Table name after the "from" in the query.
2. Is there any documentation concerning the formatting
option /*insert*/ ? Yes, I thought about using a sed like command on the results - only the point of the exercise is to have a single script solution - also I was planning to have 10 - 12 different queries in the generated script - not sure sed would be useful in this case.

Thank-you !
Chris Saxon
July 07, 2016 - 2:58 pm UTC

There's some documentation about the output formats:

http://docs.oracle.com/cd/E55747_01/appdev.41/e55591/intro.htm#RPTUG46239

But there's not any further options to customize how they look at the moment.

Chris

/*insert*/ auto generating result table name as "MYTABLE"

Steven Gonzalez, July 07, 2016 - 3:17 pm UTC

Chris and Connor... Thanks for the feedback !
Chris Saxon
July 07, 2016 - 3:50 pm UTC

Happy to help.

call /*insert*/ using command prompt

Mrunali, January 24, 2018 - 10:55 am UTC

select /*insert*/ * from Table_Name;
Uing Oracle sql developer i am able to generate insert statements for a table. Now i have to achieve same thing using command prompt.

How can i do that?

Connor McDonald
January 24, 2018 - 1:53 pm UTC

Check out sqlcl

http://www.oracle.com/technetwork/developer-tools/sqlcl/downloads/index.html

It supports the same syntax tweaks as SQL Developer

Comment as Table Name

Cesar Manzano, March 09, 2018 - 12:04 am UTC

I had the same issue trying to generate some INSERT statments from different union queries, I noticed that if you put a comment before the first FROM table , the output take the comment as the table name. I give a simple example:


SELECT /*insert*/
 A.NAME  ,
 B.ADRESS,
 C.PHONE ,
FROM 
--MY_OWN_TABLE,
 TABLE1
 TABLE2 ,
 TABLE3 ,
WHERE
A.NAME = B.NAME
AND 
A.NAME = C.NAME;


The output looks like:


REM INSERTING into --MY_OWN_TABLE
SET DEFINE OFF;
Insert into "--MY_OWN_TABLE" (NAME,ADRESS,PHONE ....



Hope it helps everyone
Greetings
César Manzano
Connor McDonald
March 09, 2018 - 2:32 am UTC

Nice touch!