Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone
I have one integer column of serial no in sql server table.
I am inserting values in it by incrementing from last one by 1
I want query such as
when i delete any one record from dot net front end then sr nos greater than that no should get decrease by one
Hope you understand

please help
Posted

That would not be a good design if you ask me. Although that might be possible, it would cause an overhead on your processing, because each delete you make will update records below it. What if you have a million records, and a user decides to delete record with serial number of 10, what do you think will happen? I suggest you let your database handle the auto numbering, instead of passing it from your app, to your database.

[Update]
Here's an example that you might want to look at.
SQL
CREATE PROCEDURE [dbo].[YourSP](@Serialnum INT)
AS
BEGIN
    DELETE FROM YourTable WHERE Serial_num = @Serialnum
    UPDATE YourTable SET Serial_num = Serialnum - 1 WHERE Serial_num > @Serialnum
END
 
Share this answer
 
v2
Comments
mayur csharp G 29-Jul-11 2:20am    
I agree but i just want to show a srno to user and i database will not too big. it is a small app. And also i cant use auto numbering identity column as it cant be updated.
Do you have solution?
walterhevedeich 29-Jul-11 2:27am    
If you say so. Take a look at my updated answer.
mayur csharp G 29-Jul-11 2:55am    
thanks it works fine
walterhevedeich 29-Jul-11 3:12am    
No worries. It was actually straightforward. I little bit of reading some tutorials will have you do that easily.
I presume that you have a PK column in the table.
Before deleting get all the PK greater than the one you are about to delete...store in array. After deletion, now you can update the all serial nos by looping through the array.

Hope it helps you.
 
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