Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i am deleting the record from the accdetails.i want the accno of the deleted record to be reused when i insert a new record in accdetails.is it possible in the programming from the win forms


thanq
Posted
Comments
Pandya Anil 23-Nov-11 6:00am    
will you delete from the end of the records only ? do you think its a physible requirement ?

You can reset the seed value of the Identity column, just fetch the maxValue from the table and reset the Identity column value to max + 1 using the following command, click the link to learn more.

SQL
DBCC CHECKIDENT (Person.Contact, reseed, 100);

http://sqlwithmanoj.wordpress.com/2011/07/08/reseed-tables-identity-column-value/[^]
 
Share this answer
 
Comments
[no name] 23-Nov-11 6:06am    
if i have 20 record with seed of 200 and increament by 1 and when i delete a record with 211 seed no and when i add a new record it should be added with 211 no implicity
First, read this:

http://stackoverflow.com/questions/4288089/reuse-identity-value-after-deleting-rows[^].

Next, I'm afraid that using identity column is not a good direction for your scenario. If you need automatic numbering with reuse of deleted numbers, you can do, for example, the following:

1) Maintain "next row ID" counter by yourself -- you can have a table in the database that will hold the latest value of the ID counter.

2) Maintain a separate table of dropped IDs. You can populate it from an AFTER DELETE trigger on your data table.

3) When adding a new row, first try to "dequeue" lowest ID from the table of dropped IDs, then if unsuccessful, increment the ID counter (you can do it in an INSTEAD OF INSERT trigger).

There are two things to bear in mind with the above solution, though.

1) If there are multiple simultaneous INSERTs to data table, you have to correctly handle concurrency of incrementing the counter and dequeueing dropped IDs. If you use SQL Server 2005 and higher, you can use CTEs for this purpose (WITH...UPDATE and WITH...DELETE statements -- look here: http://rusanu.com/2010/03/26/using-tables-as-queues/[^]).

2) If your application produces a tough load by issuing miltiple simultaneous INSERTs and DELETEs on the data table, the dropped IDs table can become a performance bottleneck. This solution would work best if you have much less DELETEs than INSERTs.
 
Share this answer
 
If accno is an auto increment field you have no choice other than doing a maintenance job periodically. It means that you should reset all of the record IDs sequentially in a way that there is no gap between them and then set the seed number with command that Pandya Anil have provided for you to the largest identity number.
And repeat this work every time that you feel the number of gaps are getting very large.

If accno is not an auto increment field then you can accumulate deleted IDs in another table and when inserting new records first pick IDs from deleted ones and if no record was there create the next largest one and use it for insertion.

And finally I invite you to consider the data type of BigInt which can be used as an autoinrement field. It has 8 bytes (64 bits) so the largest number that it can store is : 18,446,744,073,709,551,616
It means that you can insert 18 billion records every day for a 1 billion day period.
Do you really think that you may encounter a problem with having this enormous number space ?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900