Home > Archive > ASP .NET > August 2004 > primary key
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Matt Mercer 2004-08-30, 8:57 pm |
| Hi all,
Hope I am not posting too many questions...cannot find the answer
anywhere, though. I need to know when I insert a record into my
database table what the primary key is for that record. It is
generated by SQL automatically, but I need to associate data in other
tables with it.
I can pull the highest primary key # from the table but if someone
else submits info before this happens I may get the wrong number. Is
there a way to find out what they primary key assigned is as I do the
insert? Thanks again.
| |
| Hermit Dave 2004-08-30, 8:57 pm |
| if you primary key field is identity field then all you do is start a
strat trans myinsert
insert statement here
declare @myprimarykey int
select @myprimarykey == @@identity
commit trans myinsert
--
Regards,
Hermit Dave
(http://hdave.blogspot.com)
"Matt Mercer" <mattmerc@bellsouth.net> wrote in message
news:37e3a036.0408301233.702a072f@posting.google.com...
> Hi all,
>
> Hope I am not posting too many questions...cannot find the answer
> anywhere, though. I need to know when I insert a record into my
> database table what the primary key is for that record. It is
> generated by SQL automatically, but I need to associate data in other
> tables with it.
>
> I can pull the highest primary key # from the table but if someone
> else submits info before this happens I may get the wrong number. Is
> there a way to find out what they primary key assigned is as I do the
> insert? Thanks again.
| |
| Lucas Tam 2004-08-30, 8:57 pm |
| mattmerc@bellsouth.net (Matt Mercer) wrote in
news:37e3a036.0408301233.702a072f@posting.google.com:
> Is
> there a way to find out what they primary key assigned is as I do the
> insert? Thanks again.
INSERT INTO MyTable Values (1,2,3,4,5); SELECT @@IDENTITY;
Just use the statement above with a ADO.NET Command ExecuteScalar Method.
--
Lucas Tam (REMOVEnntp@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
[url]http://members.ebay.com/aboutme/ spot18/[/url]
|
|
|
|
|