We help IT Professionals succeed at work.

Insert cfquery output into another Datasource table

zlinst28
zlinst28 asked
on
751 Views
Last Modified: 2013-12-24
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>


Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2015

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.

Author

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!!
CERTIFIED EXPERT
Most Valuable Expert 2015

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

Author

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
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2015

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....
CERTIFIED EXPERT
Most Valuable Expert 2015

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>

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.