Click here to Skip to main content
6,629,885 members and growing! (20,867 online)
Email Password   helpLost your password?
Database » Database » General     Beginner License: The Code Project Open License (CPOL)

Sql Server - Sort Order Templates

By ColinBashBash

Templates to fix changes in sort-order as they occur.
SQL, SQL Server (SQL 2000, SQL 2005, SQL CE, SQL 2008), DBA
Version:7 (See All)
Posted:9 Jun 2009
Updated:11 Jun 2009
Views:2,319
Bookmarked:4 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
0 votes for this article

Introduction  

Basically,  whenever I insert/update/delete rows and they have a sort-order column, they need to be resequenced. It seems that there should be an easy template for doing this. I couldn't find a great way to do this with triggers, so I did it as code snippets to place at the beginning of your CRUD stored procedures.

Now of course, the whole point is for it to work on new tables, so I extracted out the following SSMS Script Templates. (see attached zip)

Ok, so I put the different templates in the zip. You'll press ctrl-shift-M and it'll ask you for the table name, id, parent id, is active , and sort-order columns. I used separate templates depending on whether you have an IsActive column/expression and if you have a parent id.

Ok, now if you're like me and have an IsActive bit field, then you can use the IsActive column scripts. If you're not like me, and use an IsDeleted field or just a DeletedDate field, then you'll want to use the IsActive Expression scripts, with an "IsActive expression" of "IsDeleted=0" or "DeletedDate is null". (Hope some of that makes sense)

Using the code 

Here's a simple example of what the generated code looks like when the item has a parent item and does NOT have an IsActive flag/expression. This example is for ListItem, and it assumes a 1:N relationship between List and ListItem, where ListItem has a sort-order.

-----------------------------
--Put at top of Update proc
--Expected Parameters: @ListItemId, @ListId, @SortOrder
-----------------------------
declare @oldSortOrder int
select @oldSortOrder=SortOrder from ListItem where ListItemId = @ListItemId

if @oldSortOrder < @SortOrder
begin
	update ListItem
	set SortOrder=SortOrder-1
	where ListId= @ListId and SortOrder between @oldSortOrder and @SortOrder and ListItemId <> @ListItemId
end
else
begin
	update ListItem
	set SortOrder=SortOrder+1
	where ListId= @ListId and SortOrder between @SortOrder and @oldSortOrder and ListItemId <> @ListItemId
end

-----------------------------
--Put at top of Create proc
--Expected Parameters: @ListId,@SortOrder
-----------------------------
update ListItem
set SortOrder=SortOrder+1
where ListId= @ListId and SortOrder >= @SortOrder

-----------------------------
--Put at top of Delete proc
--Expected Parameters: @ListItemId
-----------------------------
declare @oldSortOrder int
declare @ListId int
select @oldSortOrder=SortOrder,ListId=@ListId from ListItem where ListItemId = @ListItemId

update ListItem
set SortOrder=SortOrder-1
where ListId= @ListId and SortOrder > @oldSortOrder

---------------------------------------------------------------
--ReSequenceGroup----Worried About Other Processes making Changes? 
--Expected Parameters: @ListId
---------------------------------------------------------------
declare @tempTable table (newSortOrder int identity(1,1), Id int)

insert into @tempTable 
	(ID)
select ListItemId
from ListItem
where ListId = @ListId
order by SortOrder,Name

update ListItem
set SortOrder = newSortOrder
from 
	ListItem 
	inner join @tempTable t 
		on ListItem.ListItemId = t.Id

---------------------------------------------------------------
--ReSequenceAll----Worried About Other Processes making Changes? 
--Expected Parameters: 
---------------------------------------------------------------
declare @tempTable table (newSortOrder int identity(1,1), Id int, ParentId int, SortMinus int)

insert into @tempTable 
	(ID,ParentId)
select ListItemId,ListId
from ListItem
order by ListId,SortOrder,Name

update @tempTable
set SortMinus = minSort - 1
from @tempTable t 
	inner join (select ParentId, min(newSortOrder) as minSort from @tempTable group by ParentId) subQuery
		t.ParentId = subQuery.ParentId

update ListItem
set SortOrder = newSortOrder - SortMinus
from 
	ListItem 
	inner join @tempTable t 
		on ListItem.ListItemId = t.Id

Points of Interest 

History 

License

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

About the Author

ColinBashBash


Member

Location: United States United States

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Generalanother way to re-sequence a column: row_number() Pinmemberjaan33us8:24 11 Jun '09  
GeneralRe: another way to re-sequence a column: row_number() PinmemberColinBashBash8:45 11 Jun '09  
GeneralRe: another way to re-sequence a column: row_number() PinmemberColinBashBash8:53 11 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Jun 2009
Editor: Sean Ewington
Copyright 2009 by ColinBashBash
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project