• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Need Help with a SQL Insert Statement

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try to describe my situation. I have two tables, I'll call them src and dest, and I'd like to take some data from the src table and insert it into the dest table. For every row in the src table, I need to create a new row in the dest table (by an INSERT statement, I presume) and copy all of the data from that table to this table. I'd like to do something like this:



Obviously, that doesn't work, but hopefully it gives you an idea of what I'm trying to accomplish. Unfortunately, I'm not very fluent in SQL and I just don't know how to construct a statement to accomplish this.

Any suggestions are greatly appreciated.

Thanks,
Corey
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this

INSERT INTO dest (ID, V1, V2) VALUES (SELECT ID, V1,V2 from SRC);
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget it - I couldn't figure out the SQL so I just wrote a quick JDBC app to do it for me. I guess I'm just not meant to be a DBA.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
probably of no use to you now, but incase you were interested :

INSERT INTO dest (ID, V1, V2)
(SELECT ID, V1,V2 from SRC);

was what you wanted - the VALUE keyword isn't needed.
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic