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