Skip to main content
Known Participant
July 7, 2010
Question

Insert from one table into another - I searched .. :(

  • July 7, 2010
  • 2 replies
  • 3906 views

I have a query of a table of data..  I have another identical table (same fields) that I need to transfer the all the data associated with a client ID and insert it into another table.  Than, remove the data from the 1st table.

Story:  a client enters transacations into a database table called "open".   At the end of the day, he/she will run the query - print a report and click a "close" button that I need to take this data in "open" and insert it into "closed". table.

Thanks for your help.

This topic has been closed for replies.

2 replies

Participant
September 21, 2011

So in this case the "where" should work:

<cfquery datasource="mysite" name="copytable">
    INSERT INTO WeekSales
    (Total)
    INSERT INTO TodaySales
    (phone)
    select phone from users
    select Total from TodaySales
    WHERE salesdate = todaysdate  
</cfquery>

Wolfgang


Owainnorth
Inspiring
September 21, 2011

You cannot do two INSERT INTO/SELECT FROM statements in one CFQuery tag. Try this:

<cfquery>

  INSERT INTO WeekSales ( Total )

  SELECT <whatever> from <table>

  WHERE <something> = <something else>

</cfquery>

Then do the TodaySales

<cfquery>

INSERT INTO TodaySales ( Total )

SELECT <whatever> from <table>

WHERE <something> = <something else>

</cfquery>

CFQuery does not work with multiple commands being run at once.

Participant
September 22, 2011


Re: Insert from one table into another - I searched ..

You cannot do two INSERT INTO/SELECT FROM statements in one CFQuery tag. Try this:

<cfquery>

  INSERT INTO WeekSales ( Total )

  SELECT <whatever> from <table>

  WHERE <something> = <something else>

</cfquery>

Then do the TodaySales

<cfquery>

INSERT INTO TodaySales ( Total )

SELECT <whatever> from <table>

WHERE <something> = <something else>

</cfquery>

CFQuery does not work with multiple commands being run at once.


------------------------------------------------------------
Thank´s Owain for the patient correction

Wolfgang


-------------------------------------------
my sites PKV
PKV Vergleich


Inspiring
July 7, 2010

The syntax you want is:

insert into sometable

(field1, field2, etc)

select value1, value2, etc

from wherever

In you specific case, why don't you have just one table?   You could have a field called status that you simply update.

Known Participant
July 7, 2010

Thanks Dan.  (that's one of my best friends name)

I do have a field for an "update" but I need to send this data over to a table to export for another client/owner.

<insert into possales (library_id, salesdate, total)
select library_id, salesdate, total from todaysales>

After I sent the data over to this .cfm page, I checked the database and it wasn't updated or data inserted.

FYI:  I am using an access database too.

Inspiring
July 8, 2010

How many records should have been copied?  What happens if you run that same query in access?