Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Can i get any result back from the insert Query?

I am executing a insert query from my java Program. In the insert query i am passing 0
as the value for one of the fields. In the Database side that value is replaced by a unique id
( some trigger and Sequence is used for that). To get that generated id i am again doing
another query. Is there any way by which i can avoid the 2nd query and can get the generated id from the first query itself ?
0
ugarg
Asked:
ugarg
  • 2
  • 2
1 Solution
 
objectsCommented:
No an insert cannot return a result set.
0
 
objectsCommented:
If available you could use a stored procedure to perform the insert and it could return the result.
0
 
JavatmCommented:
Think there's no other way than this or what object said :

String query =  "INSERT INTO Tablename (VALUES) SELECT Values FROM YourDataBase";

Hope this helps . . .
JAVATM
0
 
JavatmCommented:
Opps should be :

String query = "INSERT INTO Tablename (VALUES) SELECT Values FROM Tablename";
0
 
evedderCommented:
String query= "Insert into tablename(id, field2, field3...fieldn)(select sequence.nextval, fieldfromtable1, fieldfromtable2, fieldfromtable3...fieldfromtablen from othertablename
)";

if you get only the id value from database

String query="Insert into tablename(id, field2, field3...fieldn)(select sequence.nextval,"+var1+","+var2+","+var3+...+varnblen+" from dual)";

where var2, var2, var3 are values defined in your application


salu2!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now