Solved

AutoInc in SQL Server

Posted on 1998-08-25
7
572 Views
Last Modified: 2008-03-04
How can i create a field like autoincrement in Access ?
I want to have a serial number e.g. 1 to 9999999 and while appending a new record it automatically increments itself by one. I think it is done by Identity , but how? How is it used. Explanations are appreciated..
0
Comment
Question by:guvencum
  • 4
  • 2
7 Comments
 
LVL 2

Expert Comment

by:odessa
ID: 1089757
CREATE TABLE dbo.TEST (
      ID int IDENTITY (1, 1) NOT NULL ,
..
)
GO

Where you set IDENTITY to be ID field autoincrement, first 1 stands for starting value, second for step, field must be not null
0
 

Author Comment

by:guvencum
ID: 1089758
I already created an identity field like it but, i use a TTable in Delphi and bounded it to a dbgrid. When i want to insert a record, it didn't put ID field a value. And when i post, it gave me an error "ID field must have a value". (I know it must have a value. And you were supposed to give it!...) When i identify ID field as Identity, i have to make it not null. This is why SQL Server gives error message. SQL Server must put a value in ID field automatically, and when i post it must save this autoincremented value in ID field. when i did it by a TQuery with SQL statement like this it worked "insert mytable1(name,surname) values('Izzet','Altinmese')". But when i did it by TTable, it didn't work. Now i use a dummy table to store last record's id. And i autoincrement it by myself in program. But i'm not sure if tens of people enters record, if it locks the record by itself (SQL Server), or do i have to do it by myself again ( by semaphores..etc.). If i do all these things, what will SQL Server do ?!...
0
 

Expert Comment

by:manish77
ID: 1089759
I faced a similar problem (in Visual Basic ). I solved it by using following code
begin transaction1
Begin transaction2
insert mytable1(name,surname) values('Izzet','Altinmese')
end transaction2
retrieve identity column value by using max(identity column) function and a where clause like name = "Izzet" and surname = "Altinmese"

eg. select max(identity col) from table1 where name =  "Izzet" and surname = "Altinmese"

this is not foolproof but it should work for most of the cases
if it works for you i will upgrade my comments to answer.

Best of luck,
Manish77

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Expert Comment

by:manish77
ID: 1089760
sorry for error in previos code please use this one
I faced a similar problem (in Visual Basic ). I solved it by using following code

begin transaction

insert mytable1(name,surname) values('Izzet','Altinmese')
end transaction

retrieve identity column value by using max(identity column) function and a where clause like name = "Izzet" and surname = "Altinmese"

eg. select max(identity col) from table1 where name =  "Izzet" and surname = "Altinmese"

this is not foolproof but it should work for most of the cases
if it works for you i will upgrade my comments to answer.

Best of luck,
Manish77
0
 

Author Comment

by:guvencum
ID: 1089761
It seems to work .
But It is something bring extra oversheads to the server
my application must be so optimized .
I already change my mind not to use data bounded controls and TTable.
Nevertheless thanks for your comment.
Change it to answer and let me appreciate it

best of luck
guvencum
0
 

Expert Comment

by:manish77
ID: 1089762
guvencum
Please reject the answer from Odessa and unlock this question so that I can post it as a answer.
Thanx,
Manish
0
 

Accepted Solution

by:
manish77 earned 20 total points
ID: 1089763
I faced a similar problem (in Visual Basic ). I solved it by using following code

begin transaction

insert mytable1(name,surname) values('Izzet','Altinmese')
end transaction

retrieve identity column value by using max(identity column) function and a where clause like name = "Izzet" and surname = "Altinmese"

eg. select max(identity col) from table1 where name =  "Izzet" and surname = "Altinmese"

this is not foolproof but it should work for most of the cases
if it works for you i will upgrade my comments to answer.

Best of luck,
Manish77
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now