Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes Problem with identity column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem with identity column" Watch "Problem with identity column" New topic
Author

Problem with identity column

faisal hameed
Ranch Hand

Joined: Jan 19, 2009
Posts: 48
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?


Regards, M Faisal Hameed
PUCIT, Pakistan
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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

Joined: Jan 19, 2009
Posts: 48
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

Joined: Jun 01, 2010
Posts: 618
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Problem with identity column
 
Similar Threads
In Oracle, write SELECT query that will return metadata information for the oracle table's columns?
Searching and matching keyword
Queries not committing in transaction
Hibernate max function
Hibernate one-to-many weirdness...