 |
|
 |
|
Next: create index taking long
|
| Author |
Message |
External

Since: May 22, 2008 Posts: 24
|
(Msg. 1) Posted: Fri Aug 01, 2008 3:58 am
Post subject: How to find next identity Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
Hi All,
Is there any way to know the next identity number the server would
probably assign for the identity column?
I'm having a table say Student with an identity column as StudentId.
How to find the next Id value the system would assign for a new
insert?
Thanks in Advance
ArunDhaJ >> Stay informed about: How to find next identity |
|
| Back to top |
|
 |  |
External

Since: May 22, 2008 Posts: 24
|
(Msg. 2) Posted: Fri Aug 01, 2008 4:31 am
Post subject: Re: How to find next identity [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Aug 22, 2004 Posts: 840
|
(Msg. 3) Posted: Fri Aug 01, 2008 6:50 am
Post subject: Re: How to find next identity [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
You might already be aware of this but I just want to emphasize that
IDENT_CURRENT() isn't intended to used in application code to anticipate the
next value that will be assigned. The actual value assigned might be
different due to can change due to concurrent inserts by other sessions.
--
Hope this helps.
Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/
"ArunDhaJ" wrote in message
> It works like a charm...
>
> Thanks Tom.....
>
>
> Regards
> ArunDhaJ >> Stay informed about: How to find next identity |
|
| Back to top |
|
 |  |
External

Since: Apr 21, 2004 Posts: 502
|
(Msg. 4) Posted: Fri Aug 01, 2008 7:19 am
Post subject: Re: How to find next identity [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Check out IDENT_CURRENT() in the BOL.
--
Tom
----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"ArunDhaJ" wrote in message
Hi All,
Is there any way to know the next identity number the server would
probably assign for the identity column?
I'm having a table say Student with an identity column as StudentId.
How to find the next Id value the system would assign for a new
insert?
Thanks in Advance
ArunDhaJ >> Stay informed about: How to find next identity |
|
| Back to top |
|
 |  |
External

Since: Jun 16, 2008 Posts: 187
|
(Msg. 5) Posted: Fri Aug 01, 2008 10:13 am
Post subject: Re: How to find next identity [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
IDENT_CURRENT() is the last identity generated, not the next one, and we
probably need to know this within the context of a specific table.
This will return the last identity inserted for the [Student] table, and by
adding the identity increment (it is not necessarily 1), we can know what the
next identity insert will 'probably' be.
select IDENT_CURRENT('Student') + IDENT_INCR( 'Student' )
However, I don't see why an application would need to predict the next
identity, and that might be reliable. What we typically need to know is what
was the last identity inserted withinin the scope of our current session.
SCOPE_IDENTITY( )
"Tom Moreau" wrote:
> Check out IDENT_CURRENT() in the BOL.
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "ArunDhaJ" wrote in message
>
> Hi All,
> Is there any way to know the next identity number the server would
> probably assign for the identity column?
>
> I'm having a table say Student with an identity column as StudentId.
> How to find the next Id value the system would assign for a new
> insert?
>
>
> Thanks in Advance
> ArunDhaJ
>
> >> Stay informed about: How to find next identity |
|
| Back to top |
|
 |  |
|
Aaron Bertrand [SQL Serve
|
External

Since: Jan 10, 2008 Posts: 2166
|
(Msg. 6) Posted: Fri Aug 01, 2008 1:48 pm
Post subject: Re: How to find next identity [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Why do you need to know the next value *before* you assign it (e.g. just
INSERT the row already)? You know that all bets are off if you wait before
inserting, right? Between the time you check and the time you actually
insert, 10 other values may have been assigned (or attempted and rolled
back), meaning the value you provided initially is no longer correct and not
the value that they will actually obtain.
On 8/1/08 6:58 AM, in article
77bef41f-b2a8-420f-af19-e676565d0c00.DeleteThis@l33g2000pri.googlegroups.com,
"ArunDhaJ" wrote:
> Hi All,
> Is there any way to know the next identity number the server would
> probably assign for the identity column?
>
> I'm having a table say Student with an identity column as StudentId.
> How to find the next Id value the system would assign for a new
> insert?
>
>
> Thanks in Advance
> ArunDhaJ >> Stay informed about: How to find next identity |
|
| Back to top |
|
 |  |
|
Aaron Bertrand [SQL Serve
|
External

Since: Jan 10, 2008 Posts: 2166
|
(Msg. 7) Posted: Fri Aug 01, 2008 1:52 pm
Post subject: Re: How to find next identity [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
> select IDENT_CURRENT('Student') + IDENT_INCR( 'Student' )
"probably" is right. Inserts that happen after this query but before the
actual INSERT takes place will ensure that a different value is actually
assigned. Rollbacks can spoil the party, too. >> Stay informed about: How to find next identity |
|
| Back to top |
|
 |  |
External

Since: Jun 16, 2008 Posts: 187
|
(Msg. 8) Posted: Fri Aug 01, 2008 2:37 pm
Post subject: Re: How to find next identity [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Yes, it called the Observer Effect or Schrödinger's integer. It has been well
documented that future indentity values are in a state of superposition, and
attempting to determine what your next identity will be in a multiuser system
almost guarantees that it will either be inserted by another user or just
mysteriously never materializes thus leaving gaps or nonsequential IDs in
your rowset.
"Aaron Bertrand [SQL Server MVP]" wrote:
> Why do you need to know the next value *before* you assign it (e.g. just
> INSERT the row already)? You know that all bets are off if you wait before
> inserting, right? Between the time you check and the time you actually
> insert, 10 other values may have been assigned (or attempted and rolled
> back), meaning the value you provided initially is no longer correct and not
> the value that they will actually obtain.
>
>
> On 8/1/08 6:58 AM, in article
> 77bef41f-b2a8-420f-af19-e676565d0c00.DeleteThis@l33g2000pri.googlegroups.com,
> "ArunDhaJ" wrote:
>
> > Hi All,
> > Is there any way to know the next identity number the server would
> > probably assign for the identity column?
> >
> > I'm having a table say Student with an identity column as StudentId.
> > How to find the next Id value the system would assign for a new
> > insert?
> >
> >
> > Thanks in Advance
> > ArunDhaJ
>
> >> Stay informed about: How to find next identity |
|
| Back to top |
|
 |  |
External

Since: Jan 14, 2008 Posts: 132
|
(Msg. 9) Posted: Fri Aug 01, 2008 7:13 pm
Post subject: Re: How to find next identity [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Eric Russell" wrote in message
> Yes, it called the Observer Effect or Schrödinger's integer. It has been
> well
> documented that future indentity values are in a state of superposition,
> and
> attempting to determine what your next identity will be in a multiuser
> system
> almost guarantees that it will either be inserted by another user or just
> mysteriously never materializes thus leaving gaps or nonsequential IDs in
> your rowset.
So either you see sql as physics or as a physic? Interesting, as sql is as
popular as quantum mechanics
www.beyondsql.blogspot.com >> Stay informed about: How to find next identity |
|
| Back to top |
|
 |  |
External

Since: May 22, 2008 Posts: 24
|
(Msg. 10) Posted: Mon Aug 04, 2008 5:58 am
Post subject: Re: How to find next identity [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Thanks all. Actually I was in need of this function for debugging an
issue, but not to use it in the program. The explanation for why not
to use in code was really awesome, it would be really useful in
future. Thanks all for your support.
Regards
ArunDhaJ >> Stay informed about: How to find next identity |
|
| Back to top |
|
 |  |
| Related Topics: | How do I find IDENTITY columns in all tables in one databa.. - Hi all, Is there a method exists that can help me to find columns associated with "IDENTITY" property in all tables in one database? Thanks.
Next Id without Identity - I'm getting a duplicate key problem with this code. I can't use a Identity column. BEGIN TRANSACTION INSERT INTO T1 (Id1, Id2, OrderNo, Data) SELECT @Id1_p, @Id2_p, ISNULL(max(OrderNo),0) + 1, @Data FROM T1 WHERE Id1 = @Id1_p AND Id2 ...
Identity Column - Is it possible to set the indentitiy flag for a column by simply using alter table/alter column in SQL 2000 and SQL 2005.
INSERT identity - If I issue a command to insert a record with ALLOW IDENTITY INSERT will I have to reset the current IDENTITY column? Thanks. David
Question about IDENTITY columns. - We have a atble TAB1[ ID IDENTITY integer, NAME VARCHAR[30], .... ]. This table is present in 2 databases Db1 and Db2. We want to keep them both in sync daily. The mapping of Name -> ID should be the same in both the tables. We allow only Db1 tables t... |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|