473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jun 27 '08 #1
5 1038

select @@identity

or

select scope_identity()
"Marko" <ma*****@hotmail.comwrote in message
news:fu**********@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

Jun 27 '08 #2
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, @@IDENTITY is a fallback, but
suffers with triggers.

Marc
Jun 27 '08 #3
In general, this is easiest when you use stored procedures, although you can
batch commands with a semi-colon (;). I would not use @@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" <ma*****@hotmail.comwrote in message
news:fu**********@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

Jun 27 '08 #4
I would not use @@IDENTITY, as you can
end up with the wrong value on a highly used system.
This is misleading; high usage doesn't impact @@IDENTITY; @@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
Jun 27 '08 #5
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" <ma**********@gmail.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
I would not use @@IDENTITY, as you can
end up with the wrong value on a highly used system.

This is misleading; high usage doesn't impact @@IDENTITY; @@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

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not...
7
by: Scott W Gifford | last post by:
Hello, I'm considering using XML to represent a stream of location information, and XPath to do queries against it. I've got most of it figured out (at least on paper), but I can't figure out...
3
by: AndyBell | last post by:
Hi all! I have an Access 2000 database for the Habiat for Humanity where I work. This is the second database I have written and it gets a bit more complex each time... I have learned much and...
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
2
by: rf | last post by:
Hey I need to be able to get the dates from last wednesday to the past tuesday on thursday or friday every week. How would I do that? For example: m t W TH F M T w th f I would need the...
17
by: michel.ank | last post by:
Hi, I'm using the class PrintLines and my last record of page aren't with the borders. Somebody can help me? Thanks,
6
by: magix | last post by:
Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line...
2
by: Kevin Burton | last post by:
I don't think I understand the last() function. I have a document that looks like: <Root> <Header>Some text</Header> <Message> <MessageID>1</MessageID> . . . . </Message>
13
by: Greg | last post by:
Most suggestions on this topic recommend to use a page footer and make it visible only on the last page. My problem is that the footer is half of the height of a page which means the detail would...
23
by: Florian Lindner | last post by:
Hello, can I determine somehow if the iteration on a list of values is the last iteration? Example: for i in : if last_iteration: print i*i else:
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.