Results 1 to 5 of 5

Thread: Insert Into...select From...

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    TNQ
    Posts
    40

    Insert Into...select From...

    Been trawling sites for a few weeks trying to find answers about my SQL and have finally come up with this SQL (VB6 and MS Access):

    Code:
    sSQL = "INSERT INTO try([mytext], [mydate]) SELECT quantity From Stores Where CatalogueNo = '" & txtSearch.Text & " ' "
    This uses two tables and drags the field 'quantity' where txtSearch = a certain value in the stores table and inserts it into the try(mytext) table. I have a field, mydate which I want to have a todays date but according to this SQL it needs to have a field from stores.

    Can I INSERT todays date INTO the try(mydate) table without having to SELECT it FROM the the Stores table?

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Insert Into...select From...

    Yes, you simply substitute the value into the select list. I'm afraid I don't know access's function to return the current date but sql server is getDate(). Assuming Access's is the same it would look something like this:-
    Code:
    sSQL = "INSERT INTO try([mytext], [mydate]) SELECT quantity, getDate() From Stores Where CatalogueNo = '" & txtSearch.Text & " ' "
    When doing a select you don't neccessarily need to select a field from a table, you can select variables, constants, functions... anything that has a scalar value.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    TNQ
    Posts
    40

    Re: Insert Into...select From...

    I thought it would throw an exception due to 'date' not being in a field. That's good to know.

    I don't think I can UPDATE one table and INSERT values INTO a different table with the same SQL though can I?

    Thanks for your help and quick reply.

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Insert Into...select From...

    I don't think I can UPDATE one table and INSERT values INTO a different table with the same SQL though can I?
    No... but your original query didn't mention that . Give us a bit more info about what you're trying to do and we'll see if we can help. Normally you have to do separate inserts and updates though.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    TNQ
    Posts
    40

    Re: Insert Into...select From...

    Quote Originally Posted by FunkyDexter
    No... but your original query didn't mention that . Give us a bit more info about what you're trying to do and we'll see if we can help. Normally you have to do separate inserts and updates though.
    I was thinking out loud, I think! The code you provide and the explanation has allowed me to do what I planned to do. Just need to create a relationship between two tables and I'm sure I'll be good to go.

    Thanks again.

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