Win a copy of Android Programming: The Big Nerd Ranch Guide this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with identity column  RSS feed

 
faisal hameed
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?
 
Tom Reilly
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!