Skip to main content
Inspiring
March 31, 2009
Question

Creating a new table on one DSN from a Select Statement on another DSN

  • March 31, 2009
  • 6 replies
  • 1475 views
I'm hoping to perform a select statement on a single table located on DSN 1 and use it to create and populate a new table on DSN 2. Is this possible? If not I could create the new table on DSN 1 to simplify the problem. I'm using ColdFusion MX7 and MySql.

Is there a way to use [queryname].columns when creating the new table or have the new table default to the columns in the select table being used to populate the table? I ask because I'm hoping to use this code with different select statements so I would always want the new table to contain the same columns as the select statement being used at the time. I also would like one additional boolean column in the second table but figure I can add that as a second step if needed.

I have looked a the INSERT statement combined with a SELECT statement so far but have not been successful. Any ideas are appreciated.
This topic has been closed for replies.

6 replies

Inspiring
April 1, 2009
Thanks! I will look into that.
Inspiring
April 1, 2009
There is another way.

Run your query once and get all the results you need.

Loop through your query and start sending mail to the spool. When you reach your specified limit, pause for the same interval your mail spool is on.

Google "coldfusion sleep" if you don't know how to pause a template.
Inspiring
April 1, 2009
Based on that answer, I see no need to create any tables at all. That includes the table that ate Manhatten which you are contemplating.

Instead, select the sql you need from the db that holds it, run it, and do whatever you need to do with the results.
Inspiring
April 1, 2009
I think you are correct about creating the other table. I wanted to avoid sending several thousand emails into the spool at one time as it backs up the other emails going out from the site as they get queued up behind this large chunk. In the past I would do this by limiting the sql to say 300 records and after each record is sent I change the value of the sent field to eliminate those records from the next time the select statement runs. I use cffile to monitor how many files are in the coldfusion spool directory and when it drops below 10 files I run the select statement again to get the new batch of 300 recordes to be sent out. This sends my bulk mail out in chunks and keeps the other automated mail from being delayed until the bulk job is finished. I guess I could just create a table with a link to the original table and the sent field I use when sending out the chunks of email and make this all a lot easier on myself. The other option would be to add the sent field to the original table but I would prefer to do it in a separate table. Thanks for helping me to think it through more.
Inspiring
April 1, 2009
Thanks to both of you for the feedback on my question. The tables that I am trying to build are for sending emails using cfmail. I have different sql statements stored in a table that are associated with the different emails that are sent. The different emails pull different fields so I wanted to avoid having to always create a table that had every possible field in it and was hoping that I could use the column names as a way to get the correct fields built. Seeing that I would need to query the schema information to get the field types it probably just makes sense to include all of the fields in the table I create and then insert the records. I was just hoping to not have to make adjustments to the table fields each time new fields are added to the select sql.
Inspiring
April 1, 2009
what is the purpose of this, if i may ask?
is it a kind of live back-up you are trying to perform?

you are probably better of doing it on the mysql end instead on in cf...
if you are not on a shared host, of course...

more to the point of your question:
a <cfquery> can only connect to ONE dsn at a time - you can't query 2
datasources with one <cfquery> tag.

a SELECT query returns an alphabetical list of columns in
queryname.columnlist variable. but that is all the info it returns about
the columns - just their names, in uppercase.

to create a copy of the table in your other dsn, you will need to know a
lot more about the columns and their data in the original table:
datatypes, value lengths, default values, charset and collation, table
type, indexes and other constraints, etc etc etc.

the only sure way to get all that data is by querying various tables in
the information_schema database (a mysql system db which holds info
about all the databases on the same mysql server) for data about that
particular db and table... it is NOT a piece of cake.

so, my .02$ for you will be: if you have access directly to mysql
utilities on your server - use them to create a sql dump of the table,
then use that dump to create that table in your other dsn.
this, actually, can be done in cf, by invoking mysqldump.exe with proper
arguments via <cfexecute> tag. check out mysql reference manual for
detailed info on using mysqldump.exe command-line interface.

hope this helps...

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Inspiring
April 1, 2009
To create a table, you need a create table query. The manual for your rbdms should have an example of the syntax. Otherwise, google "your rdbms create table". You'll also have to know your datatypes.

Depending on your requirements and resources, there are probably better options available.