Transactions & Identity Field Questions

G

Guest

Hello All,

I have the following situation.

I have a parent table and a child table.
The parent table has an identity field which
is the primary key. This primary key is also a
field in the child table for relationship purposes.

I am doing a transaction "INSERT" on "BOTH" the parent and child at the
same time by inserting then committing the transactions.

My questions is this.

If I am doing a commit on both transactions at the same time how do I get
the new identity value from the parent table and store this value with the
new child row that will be inserted with the parent at the same time?

Thanks,
Mark
 
C

Cirrosi

How you are inserting the rows?
If you are using an insert command from SqlCommand then you must read
@@identity value after the insert.
If you are adding rows to a datatable using a dataadapter, you must first
use fillschema on dataset and after calling dataadapter update method on
dataset you can directly read the identity table field.

Excuse me for my bad english.

ha scritto nel messaggio
news:[email protected]...
 
J

Jarmo Muukka

And instead of @@identity, please use scope_identity(), because if your
insert triggers an another insert, @@identity retrieves the id of the last
insert, but scope_identity() returns the one you want. @@identity works most
of the time, but using reliable scope_identity() saves you from mysterious
bugs if you add triggers later.

JMu

wrote in message
news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top