This stored procedure is executed from the GetSingleMessage stored procedure, and returns the item id of the next message in the discussion.
Definition:CREATE Procedure GetNextMessageID ( @ItemID int, @NextID int OUTPUT ) AS DECLARE @CurrentDisplayOrder as nvarchar(750) DECLARE @CurrentModule as int /* Find DisplayOrder of current item */ SELECT @CurrentDisplayOrder = DisplayOrder, @CurrentModule = ModuleID FROM Discussion WHERE ItemID = @ItemID /* Get the next message in the same module */ SELECT Top 1 @NextID = ItemID FROM Discussion WHERE DisplayOrder > @CurrentDisplayOrder AND ModuleID = @CurrentModule ORDER BY DisplayOrder ASC /* end of this thread? */ IF @@Rowcount < 1 SET @NextID = nullDatabase Tables Used:
Discussion: Each record in the Discussion table is a message in a threaded discussion, as displayed by the Discussion Portal Module. Since all Discussion modules store their record in this table, each item contains a ModuleID to permit related items to be retrieved in a single query.
An especially interesting feature of the Discussion table is the DisplayOrder field, which is used to create the threaded display of messages in the discussion. This field is calculated by concatenating a 23-character string representation of the current date and time to the DisplayOrder for this item's parent. In this way, messages can be displayed in the correct order via a simple ascending sort. This approach has an inherent limitation based upon the size of the DisplayOrder field. In this case it's 750 characters, meaning that messages nested more than 32 levels deep will not display in the correct order.
The primary key in this table is the ItemID identity field. Note that message bodies are limited to 3000 characters.