This stored procedure returns a count of all children of the top level message specified by "ItemID."
This stored procedure includes a simple trick that helps the Discussion module represent the nesting of the messages when it renders the thread. GetThreadMessages fills the Indent field with a number of HTML space characters proportional to the nesting level of each message. The Discussion module simply renders Indent field immediately before the Title field to create the visual effect of nesting.
Definition:
CREATE PROCEDURE GetThreadMessages ( @Parent nvarchar(750) ) AS SELECT ItemID, DisplayOrder, REPLICATE( ' ', ( ( LEN( DisplayOrder ) / 23 ) - 1 ) * 5 ) AS Indent, Title, CreatedByUser, CreatedDate, Body FROM Discussion WHERE LEFT(DisplayOrder, 23) = @Parent AND (LEN( DisplayOrder ) / 23 ) > 1 ORDER BY DisplayOrderDatabase 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.