Stored Procedure Properties
| Name | Value |
|---|---|
| Owner | dbo |
| Created | 2004-05-31 |
| Startup | False |
| Encrypted | False |
Creation Options
| Name | Value |
|---|---|
| QUOTED_IDENTIFIER | OFF |
| ANSI_NULLS | ON |
Parameters
| Name | DataType | Length | Type |
|---|---|---|---|
| @ForumID | int | 4 | INPUT |
| @PageSize | int | 4 | INPUT |
| @PageIndex | int | 4 | INPUT |
| @DateFilter | datetime | 8 | INPUT |
| @UserName | nvarchar | 100 | INPUT |
| @UnReadTopicsOnly | bit | 1 | INPUT |
Total: 6 parameter(s)
SQL
SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO CREATE PROCEDURE forums_GetAllTopicsPaged ( @ForumID int, @PageSize int, @PageIndex int, @DateFilter Datetime, -- Filter returned records by date @UserName nvarchar (50), @UnReadTopicsOnly bit -- 0 All / 1 Unread only ) AS DECLARE @PageLowerBound int DECLARE @PageUpperBound int -- Set the page bounds SET @PageLowerBound = @PageSize * @PageIndex SET @PageUpperBound = @PageLowerBound + @PageSize + 1 -- Create a temp table to store the select results CREATE TABLE #PageIndex ( IndexId int IDENTITY (1, 1) NOT NULL, PostID int, PostDate datetime ) IF @UnReadTopicsOnly = 0 AND @DateFilter < DateAdd(ww, -2, GetDate()) AND (@PageSize * @PageIndex) < 100 -- INSERT into the temp table INSERT INTO #PageIndex SELECT TOP 100 PostID, PinnedDate = (SELECT Max(PinnedDate) FROM Posts WHERE ThreadID = P.ThreadID) FROM Posts P WHERE PostLevel = 1 AND ForumID = @ForumID AND Approved = 1 AND ThreadDate >= @DateFilter ORDER BY PinnedDate DESC ELSE IF @UserName IS NULL OR @UnReadTopicsOnly = 0 BEGIN -- INSERT into the temp table INSERT INTO #PageIndex SELECT PostID, PinnedDate = (SELECT Max(PinnedDate) FROM Posts WHERE ThreadID = P.ThreadID) FROM Posts P WHERE PostLevel = 1 AND ForumID = @ForumID AND Approved = 1 AND ThreadDate >= @DateFilter ORDER BY PinnedDate DESC END ELSE BEGIN -- INSERT into the temp table INSERT INTO #PageIndex SELECT PostID, PinnedDate = (SELECT Max(PinnedDate) FROM Posts WHERE ThreadID = P.ThreadID) FROM Posts P WHERE PostLevel = 1 AND ForumID = @ForumID AND Approved = 1 AND ThreadDate >= @DateFilter AND P.PostID NOT IN (SELECT PostsRead.PostID FROM PostsRead WHERE PostsRead.Username = @UserName) ORDER BY PinnedDate DESC END IF @UserName IS NULL BEGIN SELECT Subject, Body, P.PostID, ThreadID, ParentID, PostDate = (SELECT Max(PostDate) FROM Posts WHERE ThreadID = P.ThreadID), ThreadDate, PinnedDate, UserName, Replies = (SELECT COUNT(*) FROM Posts WHERE P.ThreadID = ThreadID AND PostLevel != 1 AND Approved = 1), Body, TotalViews, IsLocked, IsPinned, HasRead = 0, MostRecentPostAuthor = (SELECT TOP 1 Username FROM Posts WHERE P.ThreadID = ThreadID AND Approved = 1 ORDER BY PostDate DESC), MostRecentPostID = (SELECT TOP 1 PostID FROM Posts WHERE P.ThreadID = ThreadID AND Approved = 1 ORDER BY PostDate DESC) FROM Posts P (nolock), #PageIndex PageIndex WHERE P.PostID = PageIndex.PostID AND PageIndex.IndexID > @PageLowerBound AND PageIndex.IndexID < @PageUpperBound ORDER BY PageIndex.IndexID END ELSE BEGIN IF @UnReadTopicsOnly = 1 BEGIN -- Get Unread Topics only SELECT Subject, Body, P.PostID, ThreadID, ParentID, PostDate = (SELECT Max(PostDate) FROM Posts WHERE ThreadID = P.ThreadID), ThreadDate, PinnedDate, UserName, Replies = (SELECT COUNT(*) FROM Posts WHERE P.ThreadID = ThreadID AND Approved = 1 AND PostLevel != 1), Body, TotalViews, IsLocked, IsPinned, HasRead = dbo.HasReadPost(@UserName, P.PostID, P.ForumID), MostRecentPostAuthor = (SELECT TOP 1 Username FROM Posts WHERE P.ThreadID = ThreadID AND Approved = 1 ORDER BY PostDate DESC), MostRecentPostID = (SELECT TOP 1 PostID FROM Posts WHERE P.ThreadID = ThreadID AND Approved = 1 ORDER BY PostDate DESC) FROM Posts P (nolock), #PageIndex PageIndex WHERE P.ForumID = @ForumID AND PostLevel = 1 AND ThreadDate >= @DateFilter AND P.PostID NOT IN (SELECT PostsRead.PostID FROM PostsRead WHERE PostsRead.Username = @UserName) AND P.PostId >= (select MarkReadAfter from ForumsRead where username = @UserName and forumid = @ForumID) AND P.PostID = PageIndex.PostID AND PageIndex.IndexID > @PageLowerBound AND PageIndex.IndexID < @PageUpperBound ORDER BY PageIndex.IndexID END ELSE BEGIN SELECT Subject, Body, P.PostID, ThreadID, ParentID, PostDate = (SELECT Max(PostDate) FROM Posts WHERE ThreadID = P.ThreadID), ThreadDate, PinnedDate, UserName, Replies = (SELECT COUNT(*) FROM Posts WHERE P.ThreadID = ThreadID AND Approved = 1 AND PostLevel != 1), Body, TotalViews, IsLocked, IsPinned, HasRead = dbo.HasReadPost(@UserName, P.PostID, P.ForumID), MostRecentPostAuthor = (SELECT TOP 1 Username FROM Posts WHERE P.ThreadID = ThreadID AND Approved = 1 ORDER BY PostDate DESC), MostRecentPostID = (SELECT TOP 1 PostID FROM Posts WHERE P.ThreadID = ThreadID AND Approved = 1 ORDER BY PostDate DESC) FROM Posts P (nolock), #PageIndex PageIndex WHERE P.PostID = PageIndex.PostID AND PageIndex.IndexID > @PageLowerBound AND PageIndex.IndexID < @PageUpperBound ORDER BY PageIndex.IndexID END -- Update Forum View date IF EXISTS (SELECT ForumID FROM ForumsRead WHERE ForumID = @ForumID and Username = @UserName) -- Row exists, update UPDATE ForumsRead SET LastActivity = GetDate() WHERE ForumID = @ForumID AND Username = @UserName ELSE -- Row does not exist, insert INSERT INTO ForumsRead (ForumID, Username, MarkReadAfter, LastActivity) VALUES (@ForumID, @UserName, 0, GetDate()) END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO