Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...just the few hours I've spent on this site I have learned a lot. I can now implement features that will be very useful to my company, and some of those I never knew existed..."

Geography

Where in the world do Tek-Tips members come from?
jdavenport (Programmer)
7 Jul 11 12:18
What is the fastest way to populate a table with the results of a query?  I have a join query over 2 tables in database A and I need to insert all the rows found into a table in database B.  

I cannot do it all in one query:
insert into table3 (select ... from table1, table2...);
because the tables are in different databases and postgresql does not allow inter-db queries (and the cfquery datasource can only point to one database at a time anyway).  

I'd like to use Coldfusion Query of Query to help but don't see how.  I cannot query from the query results and insert into a table all in the same cfquery (since a cfquery tag can have EITHER the datasource pointing to database table is in, OR dbtype=query to do QofQ):
insert into table3 (select * from myQuery);

As a result, I have to do the query, then loop through the query results, and each time through the loop, I insert a row of the results into the table.  This is very slow!  Is there a faster way with CF?

Thanks!
FALCONSEYE (Programmer)
7 Jul 11 15:52

CODE

INSERT into myTable (myID, field1, field2)
SELECT otherID, otherField1, otherField2
FROM myOtherTable
WHERE something = something

ColdFusion Ninja for hire.

jdavenport (Programmer)
7 Jul 11 15:57
I do use this syntax when all tables involved are in the same database.  However, in this case the tables I am querying from are in database A (postgresql 8.4) on server A and the table I need to insert into is in database B (postgresql 7.4) on server B.
Thanks.
FALCONSEYE (Programmer)
7 Jul 11 16:50
oops. why not loop over the query and do your inserts?

CODE

<cfoutput query="qry1">
    <cfquery datasource="#dsn#">
        insert into myTable (field1, field2, field3)
        values ( <cfqueryparam cfsqltype="cf_sql_varchar" value="#field1#">,
                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#field2#">,
                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#field3#"> )
    </cfquery>
</cfoutput>
 

ColdFusion Ninja for hire.

jdavenport (Programmer)
7 Jul 11 16:52
Yes, that is what I am doing, but it is very slow.  I was wondering if anyone had a better idea for a faster way.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close