Click here to Skip to main content
16,010,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

I am sakshi.
I am creating a c#.net windows based project with ms sql database.
I am using delete query which is working well.
but my problem is------
I want to re-arrange the datatable after deletion.
As I have rows having id 1,2,3,4,5,6 which is not the primary key.
after delting 1,5 row,it becomes 2,3,4,6.
I want that it should be 1,2,3,4 means remaining rows should be re-arrange staring from 1.

IS there any other way to it without looping..


Please reply.

Thanks a lot
Posted
Updated 22-Jun-10 20:34pm
v3

If this is not a primary key, the simplest way to do this would be to run a loop to update this key after every delete.
 
Share this answer
 
After Delete from your table just execute below script it will re arrange your ID

CREATE PROCEDURE INSERTID_FDA_UNII_SUBSET
AS

BEGIN
Declare @ID int
Declare @MaxID int
Declare @OtherVal nvarchar(20)

DECLARE UserDet_cursor CURSOR FOR 
	SELECT Id,Uniicode FROM Table1
open UserDet_cursor
Set @MaxID = 1
FETCH NEXT FROM UserDet_cursor into @ID,@OtherVal
while @@FETCH_STATUS =0
BEGIN
	update Table1 set ID = @MaxID where OtherVal= @OtherVal and ID = @ID
	
	FETCH NEXT FROM UserDet_cursor into @ID,@OtherVal
END
CLOSE UserDet_cursor
DEALLOCATE UserDet_cursor
END
 
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