• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Jul 07, 2010 Jul 07, 2010

Copy link to clipboard

Copied

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.

TOPICS
Advanced techniques

Views

3.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 07, 2010 Jul 07, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 07, 2010 Jul 07, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 08, 2010 Jul 08, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 08, 2010 Jul 08, 2010

Copy link to clipboard

Copied

No records were copied.  I am just wondering if I have the wrong statement. This gets frustrating.. Seriously, what is the big deal with an insert statement.  I used a CF insert instead of just a insert.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 08, 2010 Jul 08, 2010

Copy link to clipboard

Copied

I actually was using a insert and I put a CFinsert and got this error:

Invalid token ( found on line 1 at column 25.

The CFML compiler was processing:

  • The tag attribute sales, on line 1, column 16.
  • A CFinsert tag beginning on line 1, column 2.
The error occurred in xxxxxxxxxxxxxxxxxxxxx\sitesample\processsystembatch.cfm: line 1
1 : <CFinsert into sales(id, salesdate, provider)
2 : select id, salesdate from batchsales>
3 :            

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 08, 2010 Jul 08, 2010

Copy link to clipboard

Copied

Don't use <cfinsert>, as it has a specific purpose and that purpose is not what you're trying to do.

Use <cfquery>.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 08, 2010 Jul 08, 2010

Copy link to clipboard

Copied

Guys:   I have to be an idiot.  I cannot seem to get this straight.  Can someone just correct this code I have PLEASE !!

From table called TodaySales and Insert data in WeekSales

I have a record called Total in both tables.  So how do I do this?

Thanks guys and sorry to be "begging" for this...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 08, 2010 Jul 08, 2010

Copy link to clipboard

Copied

Hi

Try out this and let me know

<cfquery datasource="mysite" name="copytable">
    INSERT INTO WeekSales
    (Total)
    select Total from TodaySales
</cfquery>

replace datasource name "mysite" with your datasource name

thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

Thanks... this did work... BUT...  can I use a WHERE statement with this??

In other words: can I do a insert for a specific sales date?  Kinda like this but it doesn't work:

<cfquery datasource="mysite" name="copytable">
    INSERT INTO TodaySales 
    (phone)
    select phone from users


WHERE salesdate = todaysdate


</cfquery>


One of these tables will have all of the sales but I don't want to take ALL of the sales in this table and insert into another table.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

You say it doesn't work.  What actually happens?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

Error Executing Database Query.

[Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Syntax error (comma) in query expression '(partynumber,totalbill,foodsales,drinksales)'.
The error occurred in\sitesample\processsystembatch.cfm: line 9
7 :     select (partynumber,totalbill,foodsales,drinksales) from foodsales
8 :      
9 :      where #our_id# = #our_id#
10 : 
11 : </cfquery>

SQLSTATE  42000
SQL   INSERT INTO sales (partynumber,totalbill,foodsales,drinksales) select (partynumber,totalbill,foodsales,drinksales) from totalsales where 405672 = 405672
VENDORERRORCODE  -3100
DATASOURCE

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

I'm sure you noticed the logic error in your where clause, but, looking for the syntax error, what happens if you run this?

select (partynumber,totalbill,foodsales,drinksales) from  totalsales where 1=2

What happens if you try it without the brackets?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

I used the # # for the varibles past.  I did a where 123 = 123

and I get the same results.  I didn't realize what I was trying to do would be so difficult.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 13, 2010 Jul 13, 2010

Copy link to clipboard

Copied

You don't need the parens around the list of fieldnames in the SELECT part of the statement.  Yes, you can put a WHERE clause at the end of the whole thing.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 21, 2011 Sep 21, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 21, 2011 Sep 21, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 22, 2011 Sep 22, 2011

Copy link to clipboard

Copied

LATEST


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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation