jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes Insert a new row with values from an existing record Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Insert a new row with values from an existing record" Watch "Insert a new row with values from an existing record" New topic
Author

Insert a new row with values from an existing record

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

How can i add a new row with the values of an existing row..the only value that i will supply is the primary key..

I am not getting the sql query for that...

the database i am using is db2..

Is it possible with a single insert statement..What should i do?

Thanks in advance,


[ September 18, 2006: Message edited by: A Kumar ]
[ September 18, 2006: Message edited by: A Kumar ]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

I believe what you are looking for is called an INSERT SELECT statement. Although this can be DB specific the basic syntax is:


My Blog: Down Home Country Coding with Scott Selikoff
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks !!!

But what i want is ...

in the insert statement..i will provide the primary key value...and the rest of the columns from another row in the same table..

Regards
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
This is a near solution

insert into table
select * from table2 where table2.id=?

But this one wont allow me to specify table.primarykey value explicitly

Regards
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by A Kumar:

But this one wont allow me to specify table.primarykey value explicitly



This will certainly allow you to do anything you want, you need to tweak a little bit, assuming TABLE1_ID is your primary key



Shailesh


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Insert a new row with values from an existing record