SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    insert table from table with different structure

    My target table has two columns (client, temp).

    My source table has many columns including client.

    How do I INSERT the target table from the source table without dropping the temp column before the insert?

    I thought something like this would work , but no go.

    Code:
    INSERT INTO target (temp) VALUES (0) SELECT client FROM source
    Obviously, I can drop the temp column in the target table, INSERT from the source, and add the temp column, but I'd like to avoid that.

    Thanks.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    INSERT INTO target
    SELECT
        client
      , 0
    FROM source

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Very elegant guido2004. Thank-you.

    I played with your code and found that it worked for a single additional text input:
    Code:
    INSERT INTO target
    SELECT
        client
      , 'here'
    FROM source
    but not for TWO additional text inputs:
    Code:
    INSERT INTO target
    SELECT
        client
      , 'here'
      , 'here2'
    FROM source
    What's the notation for inserting selected data from a column in a source table into a target and filling the remaining two columns with token data?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nichemtktg View Post
    I played with your code and found that it worked for a single additional text input:
    but not for TWO additional text inputs:
    But it does (assuming the table has three columns).
    What error do you get?

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the false alarm. It must've been a obstacle illusion.

    Working fine now.

    Thank-you so much once again.


Bookmarks

Posting Permissions

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