Results 1 to 6 of 6

Thread: [RESOLVED] Best method for moving record to another table?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Resolved [RESOLVED] Best method for moving record to another table?

    I have some records I need to move from one table to another. But in the second table it has some extra fields that need to be filled in also. For example, if I sell an item, I want to move the item record to the sales table but also include the selling price, date, etc. in the extra fields.

    1. I can load all the fields from the inventory table to variables and then insert them in the Sales table with the extra fields.

    or

    2. I can use an Insert command like below and then do an Update command to update the extra fields. I wish I could do it all in one sql statement.


    VB Code:
    1. rs.Open "INSERT INTO Sales SELECT * FROM StoreInv WHERE ItemNo = '" & ItemNo2 & "'", cn


    Thanks!

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Best method for moving record to another table?

    The SQL statement can be something like this:
    INSERT INTO Sales (field1, field2, ... fieldn, salesonlyfield1, salesonlyfield2)
    SELECT field1, field2, ... fieldn, somevalue1, somevalue2
    FROM StoreInv
    WHERE ....
    "It's cold gin time again ..."

    Check out my website here.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Best method for moving record to another table?

    You can do it in one statement... and you should be doing more of what I am here; not specifying the fields (to Insert or Select) is a recipe for disaster - what happens when either table had new fields added? or exisiting fields have their positions changed?

    This is the kind of SQL you should be running already:
    Code:
    INSERT INTO Sales (field1, field2)
    SELECT fieldA, fieldB FROM StoreInv WHERE ItemNo = 'value'
    ..and this is how to "add" fields to it:
    Code:
    INSERT INTO Sales (field1, field2, field3)
    SELECT fieldA, fieldB, 3.25 as SellingPrice FROM StoreInv WHERE ItemNo = 'value'


    edit: how slow was I?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Re: Best method for moving record to another table?

    So in your example, the field3 is the added field for the sales info that I want to include? And the 3.25 as SellingPrice that you have added, is that how I would actually do it just like you typed by putting the value in as the 3.25? How would I do that for a string? Would I do it like 'Abc' as SalesClerk ? Thanks alot for your help! Saves alot of work for me and on the database.


    Code:
    INSERT INTO Sales (field1, field2, field3)
    SELECT fieldA, fieldB, 3.25 as SellingPrice FROM StoreInv WHERE ItemNo = 'value'

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Best method for moving record to another table?

    All of your assumptions are correct.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Re: Best method for moving record to another table?

    I just added it into my code and it worked perfect!!!! THANKS!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width