Insert cfquery output into another Datasource table

Hello,

I am atttempting to populate (INSERT INTO) a new datasource table from a CFOUTPUT SELECT STATEMENT(1000 records). Can anyone show me how this is done? Here is my initial query:

<cfquery name="HR_Repo" datasource="HR_Repository">
SELECT Site_Name, Site_ID, Site_Type, Manned_Unmanned
FROM denorm
</cfquery>


zlinst28Asked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
Well for a one-time import you could use sql server's import wizard (assuming you have access). If you want to do it using CF only, a simple option is to loop through the query and insert the data, one row at a time.  While this technique would not be desirable for very large or recurring inserts, it should do the job here.  

I don't know the data types of your column types, so I left that part out.  Feel free to ask if you have questions.


<cfquery name="HR_Repo" datasource="HR_Repository">
SELECT Site_Name, Site_ID, Site_Type, Manned_Unmanned
FROM denorm
</cfquery>

<cfloop query="HR_Repo">
   <cfquery name="HR_Repo" datasource="HR_Repository">
    INSERT INTO TheOtherTable (Site_Name, Site_ID, Site_Type, Manned_Unmanned)
    VALUES
    (
       <cfqueryparam value="#Site_name#" cfsqltype="The column data type here">,
       <cfqueryparam value="#Site_ID#" cfsqltype="The column data type here">,
       <cfqueryparam value="#Site_Type#" cfsqltype="The column data type here">,
       <cfqueryparam value="#Manned_Unmanned#" cfsqltype="The column data type here">
    )
    FROM denorm
   </cfquery>
</cfloop>

There are other options as well, like linkedservers, OPENQUERY(), OPENDATASOURCE(), etc. but that's probably overkill for a one-time import.

0
 
_agx_Commented:
1. What type of database are you using
2. Is the destination table in the same database?
3. Is this a one-time insert ?

I ask because there are several options for doing this type of insert, but the best method depends on the answers to those questions.

0
 
zlinst28Author Commented:

We are using a SQL SERVER 2005

The exporting table is in a different DSN than the one I am trying to import into.

This is just a one time push from the originating table into an empty table with the same field names.

Thanks for the help!!
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
_agx_Commented:
You're welcome.  

One more question, I take it the the target table is on a different sql server?  Otherwise, you just could just
the 3/4-part naming syntax like this  (assuming you had the correct permissions).

INSERT INTO seconddabase.owner.OtherTable(....Columns...)
SELECT  ... Columns...
FROM     firstdatabase.owner.SourceTable
0
 
zlinst28Author Commented:
Actually the database I am running my CFQUERY SELECT is in Access and the INSERT will be into a SQL 2005, sorry to leave that part out!

Thanks...

zack
0
 
_agx_Commented:
>  <cfquery name="HR_Repo" datasource="HR_Repository">

Correction.  The inner query should use a separate name and obviously the second datasource name

    <cfquery name="TransferDataRow" datasource="YourOtherDatasource">
       INSERT INTO TheOtherTable (Site_Name, Site_ID, Site_Type, Manned_Unmanned)
      ... etc....
0
 
_agx_Commented:
For PAQ purposes, the whole thing would be something like this

<cfquery name="HR_Repo" datasource="HR_Repository">
SELECT Site_Name, Site_ID, Site_Type, Manned_Unmanned
FROM denorm
</cfquery>

<cfloop query="HR_Repo">
   <cfquery name="TransferDataRow" datasource="YourOtherDatasource">
    INSERT INTO TheOtherTable (Site_Name, Site_ID, Site_Type, Manned_Unmanned)
    VALUES
    (
       <cfqueryparam value="#Site_name#" cfsqltype="The column data type here">,
       <cfqueryparam value="#Site_ID#" cfsqltype="The column data type here">,
       <cfqueryparam value="#Site_Type#" cfsqltype="The column data type here">,
       <cfqueryparam value="#Manned_Unmanned#" cfsqltype="The column data type here">
    )
   </cfquery>
</cfloop>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.