Solved

Retrieving the auto-generated number

Posted on 2001-09-04
6
652 Views
Last Modified: 2007-12-19
Hi geeks,

Actually I'm quite new to this, so your patience is much appreciated.

When inserting certain record that has a field as auto-generated number, one may need to return this number for later association with other records in other tables.  For example; sometimes it's impossible to distinguish the record except from this number, so if inserted, there will be no way to get back to the record unless this auto-generated number is captured right after the insertion process.

In oracle, (I don't know really if it's oracle specific or not!) I found that they provide some sort of routine(maybe it's pl/sql, I don't know) on which you can invoke ".nextval" to insert the result as an ID to the record, then invoke ".currval" to get that ID for programming use.  Frankly, I don't know if I can do such thing in MS SQL server or even how to do it, so I appreciate if you can help me in this.

I saw somewhere else that after insertion of certain record they do something like:

"SELECT @@identity"

I really don't know how such thing works, or even if it could be used in MS SQL server, but I'm in favor of using such thing (or at least that what I feel from the syntax), because when dealing with concurrent invocations to database, one user may insert a record using
"recordnum.nextval" for example, and during insertion process, another may insert another record before the first has finished, so if the first one invokes
"recordnum.currval" he will take the wrong ID.

To brief, I have two questions:

1. Can we use [routine].nextval in MS SQL server? If so, is it safe to use it (see case above)? If yes, can you please tell me how [routine] can be created in MS SQL server.

2. Is there anything called "SELECT @@identity" in MS SQL server? Do I need special instructions when creating the database to be able to use it, like saying: "set this field as identity or auto number, etc."? Does it suffer from the concurrent invocation problem exists in case of [routine].nextval?

Thank you very much for your help.

0
Comment
Question by:ymasri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:ibro
ID: 6452590
Hi ymasri@idg,
 @@IDENTITY is a global variable, which holds the last inserted identity (auto-generated number) for the last insert. you can assign in a viariable and it is valid unitil the next autogenerated insert. So for example if you have the code:

create table tst (id int identity (1,1), val varchar(10) )
go

delcare @lastid int
insert inot tst (val) ('val1')
insert inot tst (val) ('val2')
set @lastid=@@IDENTITY
print @lastid


lastid variable will be 2, but not one


0
 
LVL 3

Expert Comment

by:ibro
ID: 6452595
there is no function or stored procedure or like in oracle - sequences - to do this job. if you want you can write your own. however the best and safest method is to use the identity property of the field and use @@IDENTITY variable.
0
 

Author Comment

by:ymasri
ID: 6452686
Thank you ibro for your response.

Forgive my ignorance, but what does it mean to say identity(1,1)? It sounds something like declaring the "id" as an identity, but I don't know what is it to say "(1,1)"?

You didn't address the problem of concurrent invocation of the database, I think with using @@IDENTITY, the problem still exists.  Isn't there any solution from the database view?
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 3

Accepted Solution

by:
ibro earned 75 total points
ID: 6452809
identity (1,1) means that the field will be auto-numbered starting with 1 and increasing each next record with 1.
so the first value will be 1, next 1+1, and so on.
example identity(100,5) means that first value will be 100, next 105, them 110 and so on.
SQL server is taking care of concurent invocatio of insert. It applies row-level (sometimes table-level) locking, so it garantee that there will be no duplicate values. @@IDENTITY variable keeps the last generated identity value for the current connection, but not for all connections.
0
 
LVL 9

Expert Comment

by:miron
ID: 6452918
Well, for the control over concurrent invocations, SQL Server, as well as Oracle both do more than just a "control" ;) ( wink ) If 2 users insert 2 records and the insert is too close in time or if some really neat trick is needed, like generate and retrieve the @@identity / sequence while insert is in progress, it will return a run time error and a message, message reads, identity undefined. On the other hand, identity is a function that can be used in a select list to generate values for an insert into for example temp table. There are restrictions applied onto table/column that hold identity values, there can be only 1 column per table holding identity values. That can be serious limitation if you insert result of a complex join into a temp table, where one of the clummns is an identity in the base table, and need to rank the rows using identity function as a  value in a select list.
--- begin SQL pseudo code
select
identity( int, 0, 1 ), --- try to rank on teh fly
t1.PK_base_table_idemtity,
t2.name,
t3.address,
t2.phone
into #temp_table
from
table_1 as t1
inner join
table_2 s t2 on t1.ID = t2.ID_ref
inner join
table_3 as t3 on t3.ID_ref = t2.ID
where name like '%foo%'
order by t2.phone, t1.PK_base_table_idemtity desc

 The statement will fail with error. Error message reads ( in - exact ) only one identity column allowed per table. In all other respects - which sufficses 99.9999 of normal database life identity works perfect. By the way in the SQL Server 2000 a great wealth of functions appeared that makes identity even more robust. IDENT_CURRENT() retrieves last identity generated for a particular table IDENTITY_SCOPE will return identity generated in current block of Transact SQL code.
0
 

Author Comment

by:ymasri
ID: 6453158
Thank you miron for your contribution, I'm sure it's a reliable solution that works 99.9999% of the cases.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question