Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > return IDENTITY after SQL Insert?

Reply
Thread Tools

return IDENTITY after SQL Insert?

 
 
=?Utf-8?B?RGFiYmxlcg==?=
Guest
Posts: n/a
 
      07-20-2006
I'm using an SQLCommand to insert row using a text command. Is there a way to
return the IDENTITY key value after the insert ?

Thanks much!
 
Reply With Quote
 
 
 
 
Mischa Kroon
Guest
Posts: n/a
 
      07-20-2006

"Dabbler" <> wrote in message
news:8936C83E-99CF-4CFF-B48F-...
> I'm using an SQLCommand to insert row using a text command. Is there a way
> to
> return the IDENTITY key value after the insert ?


set SQLCommand =
insert into table values ('string value');select scope_identity()



 
Reply With Quote
 
 
 
 
Naveen
Guest
Posts: n/a
 
      07-20-2006
Naveen Bhardwaj


This can be done by :-

using @@IDENTITY which returns the last-inserted identity value

But the preferable solution is to use IDENT_CURRENT('TableName') which
returns the last identity value generated for a specified table in any
session and any scope.



"Mischa Kroon" <> wrote in message
news:6e108$44bf4cfe$3ec3ac38$. ..
>
> "Dabbler" <> wrote in message
> news:8936C83E-99CF-4CFF-B48F-...
> > I'm using an SQLCommand to insert row using a text command. Is there a

way
> > to
> > return the IDENTITY key value after the insert ?

>
> set SQLCommand =
> insert into table values ('string value');select scope_identity()
>
>
>



 
Reply With Quote
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      07-20-2006
In case anyone is curious, @@IDENTITY is dangerous 'cuz it returns the last
ID generated from any scope. If you are using triggers, look for this to
really bite you in the ass.

For example, you insert a new customer and select @@IDENTITY to get his/her
customerId...problem is you have a trigger on insert in your custom table
which adds a new audit record. You'll end up getting hte auto-generated
auditId wthout knowing it..

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/


"Naveen" <> wrote in message
news:e9o27g$p9l$...
> Naveen Bhardwaj
>
>
> This can be done by :-
>
> using @@IDENTITY which returns the last-inserted identity value
>
> But the preferable solution is to use IDENT_CURRENT('TableName') which
> returns the last identity value generated for a specified table in any
> session and any scope.
>
>
>
> "Mischa Kroon" <> wrote in message
> news:6e108$44bf4cfe$3ec3ac38$. ..
>>
>> "Dabbler" <> wrote in message
>> news:8936C83E-99CF-4CFF-B48F-...
>> > I'm using an SQLCommand to insert row using a text command. Is there a

> way
>> > to
>> > return the IDENTITY key value after the insert ?

>>
>> set SQLCommand =
>> insert into table values ('string value');select scope_identity()
>>
>>
>>

>
>



 
Reply With Quote
 
tfsmag
Guest
Posts: n/a
 
      07-20-2006
I had a similar issue to what Karl mentioned, was lucky enough to catch
it while still in development. I ended up doing away with using
triggers for auditing purposes and just made the audit table insert at
the end of the stored proc.


Karl Seguin [MVP] wrote:
> In case anyone is curious, @@IDENTITY is dangerous 'cuz it returns the last
> ID generated from any scope. If you are using triggers, look for this to
> really bite you in the ass.
>
> For example, you insert a new customer and select @@IDENTITY to get his/her
> customerId...problem is you have a trigger on insert in your custom table
> which adds a new audit record. You'll end up getting hte auto-generated
> auditId wthout knowing it..
>
> Karl
>
> --
> http://www.openmymind.net/
> http://www.fuelindustries.com/
>
>
> "Naveen" <> wrote in message
> news:e9o27g$p9l$...
> > Naveen Bhardwaj
> >
> >
> > This can be done by :-
> >
> > using @@IDENTITY which returns the last-inserted identity value
> >
> > But the preferable solution is to use IDENT_CURRENT('TableName') which
> > returns the last identity value generated for a specified table in any
> > session and any scope.
> >
> >
> >
> > "Mischa Kroon" <> wrote in message
> > news:6e108$44bf4cfe$3ec3ac38$. ..
> >>
> >> "Dabbler" <> wrote in message
> >> news:8936C83E-99CF-4CFF-B48F-...
> >> > I'm using an SQLCommand to insert row using a text command. Is there a

> > way
> >> > to
> >> > return the IDENTITY key value after the insert ?
> >>
> >> set SQLCommand =
> >> insert into table values ('string value');select scope_identity()
> >>
> >>
> >>

> >
> >


 
Reply With Quote
 
bartekm bartekm is offline
Junior Member
Join Date: Aug 2008
Posts: 7
 
      03-02-2011
Just in case you want a full example using the ReturnValue method on either an SqlDataSource or ObjectDataSource then you can find one here
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
identity impersonate=true masks the identity of the app pool for trusted sql connections Popezilla ASP .Net Security 2 03-18-2007 03:39 AM
Membership functions connect to Sql Server as Process Identity, not user identity?? andrew.sher@gmail.com ASP .Net Security 6 04-16-2006 10:37 PM
HttpContext.Current.User.Identity.Name AND Context.User.Identity.Name; nalbayo ASP .Net 2 11-11-2005 11:12 PM
Issue with Identity Impersonation and user identity used passed for trusted SQL connection. Frederick D'hont ASP .Net Security 0 07-25-2005 02:41 PM
Difference between HttpContext.Current.User.Identity and identity Impersonation Giovanni Bassi ASP .Net 0 10-20-2003 02:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57