How to gei Last ID

When I write new record with INSERT INTO i need to get ID (Autonumber, key
of this table) from this, just written record. How to do that in ASP.NET (VB
or C#) and SQL Server?

Thanks
Marko [ Fr, 18 April 2008 14:26 ] [ ID #1945118 ]

Re: How to gei Last ID

How are you doing the INSERTs? Basically, you just want to look at
SCOPE_IDENTITY() immediately after the INSERT; you could SELECT it, you
could RETURN it, or you could SET it into an OUT variable (I favor the
latter). In older versions of SQL-Server, [at] [at] IDENTITY is a fallback, but
suffers with triggers.

Marc
Marc Gravell [ Fr, 18 April 2008 14:32 ] [ ID #1945120 ]

Re: How to gei Last ID

select [at] [at] identity

or

select scope_identity()


"Marko" <marko_a [at] hotmail.com> wrote in message
news:fua46k$8qd$1 [at] sunce.iskon.hr...
> When I write new record with INSERT INTO i need to get ID (Autonumber, key
> of this table) from this, just written record. How to do that in ASP.NET
> (VB or C#) and SQL Server?
>
> Thanks
>
Steven Platt [ Fr, 18 April 2008 14:29 ] [ ID #1945121 ]

Re: How to gei Last ID

In general, this is easiest when you use stored procedures, although you can
batch commands with a semi-colon (;). I would not use [at] [at] IDENTITY, as you can
end up with the wrong value on a highly used system. SCOPE_IDENTITY() is
better.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
"Marko" <marko_a [at] hotmail.com> wrote in message
news:fua46k$8qd$1 [at] sunce.iskon.hr...
> When I write new record with INSERT INTO i need to get ID (Autonumber, key
> of this table) from this, just written record. How to do that in ASP.NET
> (VB or C#) and SQL Server?
>
> Thanks
>
NoSpamMgbworld [ Fr, 18 April 2008 16:29 ] [ ID #1945133 ]

Re: How to gei Last ID

> I would not use [at] [at] IDENTITY, as you can
> end up with the wrong value on a highly used system.

This is misleading; high usage doesn't impact [at] [at] IDENTITY; [at] [at] IDENTITY is
limited to the current spid, but problems arise if an INSERT trigger
does one-or-more INSERTs - as you get the last identity on the spid,
which might be frmo an audit table. SCOPE_IDENTITY() resolves this by
getting the last identity (on the spid) for the current context - i.e.
the INSERT you just performed.

High usage does, however, affect IDENT_CURRENT(<table name>) - but this
should not really be used in transactional code - just from maintenance
scripts etc.

Marc
Marc Gravell [ Fr, 18 April 2008 16:37 ] [ ID #1945138 ]

Re: How to gei Last ID

Not trying to mislead, so thanks for the input. I have added that to my
knowledge base.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
"Marc Gravell" <marc.gravell [at] gmail.com> wrote in message
news:%23B8zIHWoIHA.264 [at] TK2MSFTNGP05.phx.gbl...
> > I would not use [at] [at] IDENTITY, as you can
>> end up with the wrong value on a highly used system.
>
> This is misleading; high usage doesn't impact [at] [at] IDENTITY; [at] [at] IDENTITY is
> limited to the current spid, but problems arise if an INSERT trigger does
> one-or-more INSERTs - as you get the last identity on the spid, which
> might be frmo an audit table. SCOPE_IDENTITY() resolves this by getting
> the last identity (on the spid) for the current context - i.e. the INSERT
> you just performed.
>
> High usage does, however, affect IDENT_CURRENT(<table name>) - but this
> should not really be used in transactional code - just from maintenance
> scripts etc.
>
> Marc
NoSpamMgbworld [ Fr, 18 April 2008 17:32 ] [ ID #1945151 ]
Microsoft » microsoft.public.dotnet.framework.aspnet » How to gei Last ID

Vorheriges Thema: Prompt for password
Nächstes Thema: Using CookieContainer with Response.Redirect?