Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use Sequence using sql commands

 
viral patel
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to insert data to my Accounts table. I have Created AccountId_sequence to generate accountID. I'm getting data from a form and and storing in database.Here how i'm trying to insert new data with sequence, but its not updating table. Can someone show me right way to do this.
 
Rajeshwari Natarajan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can retrieve the sequence value separately like this

store the value in a variable and use it in ur insert statement.
 
SJ Adnams
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
viral,
Ignore Rajeshwari your syntax looks ok to me.
try a commit(); on the connection?
 
Jaime Garcia
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u are using
stmt.executeUpdate (" INSERT INTO Accounts (firstName, lastName, mailAdress, Zipcode) VALUES (accountID_sequence.NEXTVAL,'"+firstName+"','"+lastName+"','"+address+"','"+zipCode+"')");
But, the number of columns are diferent to the number of values, I mean, you should use :
(id, firstName, lastName, mailAdress, Zipcode)
where id is the name of the column where you want to insert the value of the sequence.
Hope this helps.
 
SJ Adnams
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well spotted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic