• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Problem with identity column

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table with one coulumn "id" as identity with seed 1 and increament 1.
when i run this query select ident_current('Account') + ident_incr('Account');
it returns 2.
when i insert one record, the value inserted in id column is 1.
when i run this query again select ident_current('Account') + ident_incr('Account');
it returns 2 again.
i am using this id column as a foreign key in another table.and this query causes error.

How i can solve this problem?
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you are using the MS SQL database. Into what table are you inserting rows? Please post the insert SQL. Also, you say the following SQL returns 2:Why are you adding these numbers together? Wouldn't you want to know the value of each of these numbers?
 
faisal hameed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to get next identity, that is to be inserted next time and to use as a foreign key in another table.
For example when i execute this query
String sql = "select IDENT_CURRENT('Voucher') + IDENT_INCR('Voucher')";
it returns 2 first time.
when i execute this query insert into Voucher values(been.getDate);
Then first row of Voucher table is
id VoucherDate

1 1/1/2010

When i execute this query insert into VoucherLineData values(been.getVoucherId , been.getProductId , been.getPrice)
insert into VoucherLineData values(been.getVoucherId , been.getProductId , been.getPrice)

id VoucherId ProductId Price

1 2 abc 300
2 2 def 250
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the following:
1. Your code
2. The complete error
3. The SQL that you used to create the two tables

Also, please UseCodeTags for maxiimum readability.
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic