Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

IDENT_CURRENT and empty table
Hi,

can somebody explain me, why the IDENT_CURRENT from an empty table is 1?
After insert of the first record it is still 1 (assuming that start value
is 1) which is okay. But if i check the IDENT_CURRENT from a newly created
table the result should be NULL, or not?

bye,
Helmut

Report this thread to moderator Post Follow-up to this message
Old Post
helmut woess
09-18-05 02:24 PM


Re: IDENT_CURRENT and empty table
>> But if i check the IDENT_CURRENT from a newly created table the result sh
ould be NULL, or not? <<

What would a NULL value mean?  Why not zero?  Remember IDENTITY is
proprietary and non-relational, so it can do anything it wants.

Think about how it would work if you had a 1950's sequential tape file
system.  The counter starts at one and is incremented after the
insertion instead of before.  It is the position of the read/write head
of the maganeitc tape drive.


Report this thread to moderator Post Follow-up to this message
Old Post
--CELKO--
09-18-05 04:23 PM


Re: IDENT_CURRENT and empty table
Am 18 Sep 2005 07:57:39 -0700 schrieb --CELKO--:
 
>
> What would a NULL value mean?  Why not zero?  Remember IDENTITY is
> proprietary and non-relational, so it can do anything it wants.
>
> Think about how it would work if you had a 1950's sequential tape file
> system.  The counter starts at one and is incremented after the
> insertion instead of before.  It is the position of the read/write head
> of the maganeitc tape drive.

We have NULL to mark something as unknown. Why not using it?
In the manual IDENT_CURRENT is described as "gives back the last generated
ident value". So from my logical point of view it cannot give back a value
if it has not generated a value before. And because zero can be a valid
starting value it can't give back zero.
Maybe it would be better we have a IDENT_NEXT which shows the next value
which will be used, instead of IDENT_CURRENT. This could avoid such
misconception.

bye,
Helmut




Report this thread to moderator Post Follow-up to this message
Old Post
helmut woess
09-18-05 04:23 PM


Re: IDENT_CURRENT and empty table
http://support.microsoft.com/defaul...kb;en-us;835188

--
Hope this helps.

Dan Guzman
SQL Server MVP

"helmut woess" <hw@iis.at> wrote in message
news:1q6jrdv4416ek.9i6nx8fks7bc$.dlg@40tude.net...
> Hi,
>
> can somebody explain me, why the IDENT_CURRENT from an empty table is 1?
> After insert of the first record it is still 1 (assuming that start value
> is 1) which is okay. But if i check the IDENT_CURRENT from a newly created
> table the result should be NULL, or not?
>
> bye,
> Helmut



Report this thread to moderator Post Follow-up to this message
Old Post
Dan Guzman
09-18-05 06:26 PM


Re: IDENT_CURRENT and empty table
Am Sun, 18 Sep 2005 17:58:46 GMT schrieb Dan Guzman:

> http://support.microsoft.com/defaul...kb;en-us;835188

Hmmm, i am not sure what to do with this information. If it is a bug why
was it not fixed in one of the next service packs?
And there is no definition what the result will be after fixing the bug.
And i did not find any information if this bug is fixed in SQL2005.

In the meantime i wrote an UDF to check if the table is empty and then give
me the right result. So it doesn't matter if Microsoft fixes the bug or
not.

but thanks for your information,
Helmut

Report this thread to moderator Post Follow-up to this message
Old Post
helmut woess
09-19-05 04:23 PM


Re: IDENT_CURRENT and empty table
helmut woess (hw@iis.at)  writes:
> Am Sun, 18 Sep 2005 17:58:46 GMT schrieb Dan Guzman: 
>
> Hmmm, i am not sure what to do with this information. If it is a bug why
> was it not fixed in one of the next service packs?
> And there is no definition what the result will be after fixing the bug.
> And i did not find any information if this bug is fixed in SQL2005.

The behaviour is the same in SQL 2005.

But the funny thing is that Books Online for SQL 2005 explicitly says that
you should get NULL back in this case...
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp


Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
09-20-05 01:24 AM


Re: IDENT_CURRENT and empty table
As Erland mentioned, the bug hasn't yet been addressed.  Your UDF
work-around is probably the best apporach.

May I ask why you need to use IDENT_CURRENT?  The function has a global
scope so it needs to be used with caution in a multi-user environment.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"helmut woess" <hw@iis.at> wrote in message
news:1vrze3n4agi4u.1lx3hgebacr8k.dlg@40tude.net...
> Am Sun, 18 Sep 2005 17:58:46 GMT schrieb Dan Guzman:
> 
>
> Hmmm, i am not sure what to do with this information. If it is a bug why
> was it not fixed in one of the next service packs?
> And there is no definition what the result will be after fixing the bug.
> And i did not find any information if this bug is fixed in SQL2005.
>
> In the meantime i wrote an UDF to check if the table is empty and then
> give
> me the right result. So it doesn't matter if Microsoft fixes the bug or
> not.
>
> but thanks for your information,
> Helmut



Report this thread to moderator Post Follow-up to this message
Old Post
Dan Guzman
09-20-05 02:23 PM


Re: IDENT_CURRENT and empty table
Erland Sommarskog (esquel@sommarskog.se)  writes:
> helmut woess (hw@iis.at) writes: 
>
> The behaviour is the same in SQL 2005.
>
> But the funny thing is that Books Online for SQL 2005 explicitly says that
> you should get NULL back in this case...

Got an update: Books Online for 2005 is wrong and will be updated.
The current behaviour will not change, because it could cause compatibility
issues.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp


Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
09-21-05 01:23 AM


Re: IDENT_CURRENT and empty table
Am Tue, 20 Sep 2005 12:39:32 GMT schrieb Dan Guzman:

> As Erland mentioned, the bug hasn't yet been addressed.  Your UDF
> work-around is probably the best apporach.
>
> May I ask why you need to use IDENT_CURRENT?  The function has a global
> scope so it needs to be used with caution in a multi-user environment.

Hello,

sorry for delay, but i was tied up with vacation :-))
The reason why i read this value in my application is as following:
All records own an identity field (which is always the primary key), which
is used to log all data activities in a log table which has an identity
field too. When i add a new record, then this record gets its identity
value and the trigger afterwards logs this insert into the log table which
creates a new record in the log table. But this insert in the log table
changes @@IDENTITY, and the ado component in my application gets this value
as new identity value, not the value from the original insert - i needed
some days to find out this behaviour. And my grid for displaying the data
jumps to the wrong record ...
So i read this value before i do the post and after posting the data i do a
refresh of the data and a repositioning in the grid to the identity value i
got with reading ident_current before.
So if something goes wrong the worst case could be that the grid shows a
wrong record. But because in my application every record has a lot of data
to set and everything has to be done by hand and there are max. 3 users at
the same time adding records into the same table i can live with this risk.

bye,
Helmut


Report this thread to moderator Post Follow-up to this message
Old Post
helmut woess
09-27-05 12:23 PM


Re: IDENT_CURRENT and empty table
Assuming you are using SQL Server 2000 then use SCOPE_IDENTITY() rather
than @@IDENTITY. SCOPE_IDENTITY won't be affected by an insert in the
trigger.

--
David Portas
SQL Server MVP
--


Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
09-27-05 12:23 PM


Sponsored Links





Last Thread Next Thread
Pages (2): [1] 2 »
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 12:56 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006