Click here to Skip to main content
15,888,166 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i have a table with colmn of serial no. whose indentity specification i have define starting from 101
but every time i use simple query of delete..
i.e. delete from tablename where serialno=@serialno
it deletes that id
i want..that after deleting it shud be assigned to next serial no.....how can i do that..please suggest some query about the fACT.....

HERE SERIAL NO COULMN IS A PRIMARY KEY THAT I HAVE DEFINED/...
sOME BODY PLEASE HELP ME IN THIS
Posted
Updated 2-Dec-10 4:15am
v2
Comments
Sandeep Mewara 2-Dec-10 10:23am    
Not clear.
Corporal Agarn 2-Dec-10 11:58am    
In the future please try to get a better title. I had no idea as to the question from this title.

You should not be using serial numbers as keys in your database - but that's a discussion for another time.

What you want to do is not scalable. If you want to reuse a serial number that has already been passed, you'll have to add that serial number to a table that only contains serial numbers to be reused.

Next, you'll have to add a stored procedure that generates and returns serial numbers. This SP would have to check for an available serial number in the table and if one isn't found, would have to generate the next serial number. The problem is that two machines can run this SP at the same time and come up with the same serial number.

In other words, you shouldn't be recycling serial numbers at all.
 
Share this answer
 
Comments
Aksh@169 2-Dec-10 11:51am    
SO HOW CAN I DO THAT SO HELP...PLEASE
Hi,
If possible please change the schema of database. You should not use serial no. as PK. You can have u r own id instead.
Like, In AutoIncrement for an Primary key of a table (type int) will increment id by 1 , and "when u delete that row it will delete that id(ofcourse with row)." and now when u insert new row it will "skip the deleted id and adds new one which is incremented."

e.g.
======== Initially=========
ID
1 .......
2 .......
3 .......
===========================
delete command for row 3.
=========After delete======
ID
1 .......
2 .......
===========================
insert command for new row
=========After insert======
ID
1 .......
2 .......
4 .......



"Checkout that 3 will not happen again."
Some thing like this can be happening with u r db also.:thumbsup:
 
Share this answer
 
I already told you how to do it.
 
Share this answer
 

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