Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 shift id of row after delete row

Author  Topic 

veronika.np
Starting Member

29 Posts

Posted - 2011-10-10 : 14:20:07
hi friends,i have a question.i have a table that it has id,name,rownumber.rownumber is int and it has 1,2,3,4,.. numbers.when i want to insert in table i get max(rownumber) and insert max(rownumber)+1 for new row.but i want to when delete a row ,for example when delete a row that rownumber is 3 i want to refresh rownumber and shift rownumber.how can ido it?please help me.
thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-10 : 14:44:04
why should you be concerned on gaps? Ideally you shouldnt worried on gaps as you can still retrieve records in order you want. but if you really want to do it see below

http://weblogs.sqlteam.com/peterl/archive/2009/03/10/How-to-efficiently-reuse-gaps-in-identity-column.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

veronika.np
Starting Member

29 Posts

Posted - 2011-10-10 : 14:48:57
no,my rownumbere is not identity,and i should not have a gap for this field(rownumber) .i set value for this field ,for first row set rownumber=1 and for another row get max(rownumber) and set max(rownumber)+1.
i want to know how can shift them?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-11 : 01:24:21
quote:
Originally posted by veronika.np

no,my rownumbere is not identity,and i should not have a gap for this field(rownumber) .i set value for this field ,for first row set rownumber=1 and for another row get max(rownumber) and set max(rownumber)+1.
i want to know how can shift them?


if its not identity then wats the problem? you can always check max value and add 1 to it right
if you're deleting out of sequence also the logic in posted link will help you to shift records to empty positions

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-11 : 01:28:48
Since you are using SQL 2005, you can make use of row_number() window function to create a continuous row number in your select query


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -