guvencum
asked on
AutoInc in SQL Server
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..
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..
ASKER
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 ?!...
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
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
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
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
ASKER
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
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
guvencum
Please reject the answer from Odessa and unlock this question so that I can post it as a answer.
Thanx,
Manish
Please reject the answer from Odessa and unlock this question so that I can post it as a answer.
Thanx,
Manish
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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