473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Equivalent to SQL Anywhere GET_IDENTITY?

Is there an equivalent in mssql to SQL Anywhere's GET_IDENTITY which
reserves the next autoinc value for a table? Yes I know about
@@Identity and SCOPE_IDENTITY. I need to get the next autoinc value
_before_ I insert the record due to the way the existing application
works.

I've seen DBCC CHECKIDENT mentioned but that seems kludgy to me.

TIA,
Jim

Nov 27 '06 #1
13 5668
Jim C wrote:
Is there an equivalent in mssql to SQL Anywhere's GET_IDENTITY which
reserves the next autoinc value for a table? Yes I know about
@@Identity and SCOPE_IDENTITY. I need to get the next autoinc value
_before_ I insert the record due to the way the existing application
works.

I've seen DBCC CHECKIDENT mentioned but that seems kludgy to me.

TIA,
Jim
You cannot reliably determine the next IDENTITY value, except maybe in
a single user system. There are other ways however:

http://www.sqlmag.com/Article/Articl...ver_48165.html

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Nov 27 '06 #2
Jim C (ji**********@g mail.com) writes:
Is there an equivalent in mssql to SQL Anywhere's GET_IDENTITY which
reserves the next autoinc value for a table? Yes I know about
@@Identity and SCOPE_IDENTITY. I need to get the next autoinc value
_before_ I insert the record due to the way the existing application
works.
The function ident_current() is the one you are looking for, but the value
it returns is global to all processes, so if you call ident_current() ,
insert a row and then look at scope_identity( ) you may see a different
value, if another process was at it at the same time.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 28 '06 #3
I don't subscribe to that site and can't see past the first page.

David Portas wrote:
You cannot reliably determine the next IDENTITY value, except maybe in
a single user system. There are other ways however:

http://www.sqlmag.com/Article/Articl...ver_48165.html
Nov 28 '06 #4
Nope, ident_current() won't work for me because it does not reserve the
next autoinc value. It just peeks at what was inserted last. The help
says it "Returns the last identity value generated for a specified
table or view in any session and any scope." I can not see how it
could reliably be used to reserve the next autoinc value in a
multi-user system because by the time you read the value, increment it,
and insert a new record another session could easily have read the same
value and inserted a row with the value you're about to insert.

The application does its own autoinc-like stuff now but fails with a
modern sql server because it relies on the old database engine's very
different locking methods. I think what I'll explore now is getting
the id source column to be read and written inside a serializable
transaction.

Erland Sommarskog wrote:
The function ident_current() is the one you are looking for, but the value
it returns is global to all processes, so if you call ident_current() ,
insert a row and then look at scope_identity( ) you may see a different
value, if another process was at it at the same time.
Nov 28 '06 #5
On 28.11.2006 00:29, Jim C wrote:
Is there an equivalent in mssql to SQL Anywhere's GET_IDENTITY which
reserves the next autoinc value for a table? Yes I know about
@@Identity and SCOPE_IDENTITY. I need to get the next autoinc value
_before_ I insert the record due to the way the existing application
works.

I've seen DBCC CHECKIDENT mentioned but that seems kludgy to me.
You do not post where you need that information. If your insert is in a
stored procedure then SCOPE_IDENTITY after the fact should work.

If for some other reasons you really need the value beforehand, you can
emulate an Oracle sequence

-- test script
create table counter (
cnt int identity(1,1) primary key clustered
)

insert into counter default values
print 'ident: ' + cast(scope_iden tity() as varchar)
-- optional: truncate table counter

insert into counter default values
print 'ident: ' + cast(scope_iden tity() as varchar)
-- optional: truncate table counter

drop table counter
The you can pull identity values from that table and use them for the
insert.

Kind regards

robert
Nov 28 '06 #6
Robert,

I'm accessing the tables with an application that uses ODBC for now
(it'll probably go some other more direct route in the near future as
things are refactored). The way it is architected it needs to know the
value before the insert occurs because of the how and when it passes
that value to child records in master-detail setups.

Your idea about emulating an Oracle sequence looks like it will be just
the ticket. Thanks!

Jim
Robert Klemme wrote:
You do not post where you need that information. If your insert is in a
stored procedure then SCOPE_IDENTITY after the fact should work.

If for some other reasons you really need the value beforehand, you can
emulate an Oracle sequence
Nov 28 '06 #7
On 28.11.2006 17:51, Jim C wrote:
I'm accessing the tables with an application that uses ODBC for now
(it'll probably go some other more direct route in the near future as
things are refactored). The way it is architected it needs to know the
value before the insert occurs because of the how and when it passes
that value to child records in master-detail setups.
Hm, I smell data inconsistency here. You certainly do not have foreign
keys on that id column, do you? Otherwise the DB would force you to
first insert the record that gets the id and then dependent records.
Your idea about emulating an Oracle sequence looks like it will be just
the ticket. Thanks!
You're welcome!

robert
Nov 28 '06 #8
On Tue, 28 Nov 2006 16:16:00 +0100, Robert Klemme wrote:

(snip)
>If for some other reasons you really need the value beforehand, you can
emulate an Oracle sequence

-- test script
create table counter (
cnt int identity(1,1) primary key clustered
)

insert into counter default values
print 'ident: ' + cast(scope_iden tity() as varchar)
-- optional: truncate table counter
(snip)

Hi Robert,

I don't hink the optional TRUNCATE is a good idea, since TRUNCATE also
resets the identity seed :-)

If you don't want to fill up the table, here's a different suggestion
(blatantly stolen from Itzik Ben-Gan's excellent book):

BEGIN TRAN;
SAVE TRAN S1;
INSERT INTO counter DEFAULT VALUES;
SET @ident = SCOPE_IDENTITY( );
ROLLBACK TRAN S1;
COMMIT TRAN;

The BEGIN TRAN and COMMIT TRAN are necessary for the SAVE TRAN and the
ROLLBACK with named savepoint to work. If you're already in a
transaction, the BEGIN TRAN will increase the tran counter and the
COMMIT TRAN will decrease it again. And the SAVE TRAN S1 / ROLLBACK TRAN
S1 combo ensures that the insert is undone (but the increment to the
identity seed and the variable assignment are left intact).

--
Hugo Kornelis, SQL Server MVP
Nov 28 '06 #9
!!!
Bad side effect: TRUNCATE TABLE resets the identity column to 1. Dumb,
poorly documented feature. Needs to have an optional NO IDENTITY RESET
clause.

So I have to use DELETE instead, that's ok.

Nov 28 '06 #10

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

Similar topics

17
3549
by: Just | last post by:
While googling for a non-linear equation solver, I found Math::Polynomial::Solve in CPAN. It seems a great little module, except it's not Python... I'm especially looking for its poly_root() functionality (which solves arbitrary polynomials). Does anyone know of a Python module/package that implements that? Just
12
2628
by: Gary Nutbeam | last post by:
I've noticed that the Perl camp has a very nice web/database environment called Maypole. Ruby has the Rails environment which on the surface seems similar to Maypole. I can't find anything in Python that ties a database to a web interface anywhere near as well as Ruby on Rails or Maypole. I see the behemoth Zope having the best web/database integration, but unfortunately I don't want to spent weeks writing xml for the interface. Does...
7
2652
by: - | last post by:
in oracle there is a '%TYPE' to reference the data type of another column. is there an equivalent in mysql? thank you.
0
1247
by: Breck Carter | last post by:
Does DB2 UDB 8.x have any feature equivalent to the LOGIN_PROCEDURE option in iAnywhere Solutions SQL Anywhere? (see description below) I want to execute a SET SCHEMA statement whenever a new connection starts. Breck SQL Anywhere Studio 9 Developer's Guide http://www.risingroad.com/SQL_Anywhere_Studio_9_Developers_Guide.html =====
1
2889
by: Ulf | last post by:
Hi, I'm currently working on automated tests where we formerly used Visual Test. Is there anywhere an equivalent to the IntStr(string a, string b) function? It does exist in VB, but apparently not in C# ? Is there any special reason or convenient replacement ? thx , Ulf
8
2177
by: Mark Rae | last post by:
Hi, Another stupid newbie question from me, I'm sorry to say... but can anyone tell me how to simulate the concept of a global constant in a C# Windows app? The app in question contains several forms, each of which need to interrogate the value of a "global" constant. Do I have to create a class with a public constant declaration and instantiate that class from each form?
7
1122
by: TJS | last post by:
is there a server side equivalent to this ?? Inherits System.Windows.Forms.Button
2
309
by: Peter Osawa | last post by:
Hi, In VB6 I was used to add procedure headers with MZtools, and other goodies... Is there an addin floating anywhere to add headers and others texts ? TIA
32
4058
by: Andrew Poulos | last post by:
I'm writing some ASP using js and I need to do a case sensitive SQL select. Googling gave me this: SELECT * FROM User WHERE Strcomp("Blue",,vbBinaryCompare)=0 Strcomp is from vbs. Is there a js equivalent, or some other way to handle this? Andrew Poulos
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.