• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Need Help with a SQL Insert Statement

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
Trust God, but always tether your camel... to this tiny ad.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic