ALTER PROCEDURE dbo.cs_forums_Posts_PostSet
(
@PostID int,
@PageIndex int,
@PageSize int,
@SortBy int,
@SortOrder bit,
@UserID int,
@ReturnRecordCount bit,
@AllowUnapproved bit = 0,
@SettingsID int
)
AS
SET Transaction Isolation Level Read UNCOMMITTED
/*设置事务隔离级别,Read UNCOMMITTED 执行脏读或 0 级隔离锁定,这表示不发出共享锁,
也不接受排它锁。当设置该选项时,可以对数据执行未提交读或脏读;在事务结束前可以更
改数据内的数值,行也可以出现在数据集中或从数据集消失。该选项的作用与在事务内所有
语句中的所有表上设置 NOLOCK 相同。这是四个隔离级别中限制最小的级别。*/
BEGIN
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
DECLARE @ThreadID int
DECLARE @SectionID int
-- First set the rowcount
-- 首先设置行数
DECLARE @RowsToReturn int
SET @RowsToReturn = @PageSize * (@PageIndex + 1)
SET ROWCOUNT @RowsToReturn
-- Set the page bounds
-- 设置也面绑定
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
-- Get the ThreadID
-- 获取主题ID
SELECT
@ThreadID = ThreadID,
@SectionID = SectionID
FROM
cs_Posts
WHERE
PostID = @PostID and SettingsID = @SettingsID
-- Is the Forum 0 (If so this is a private message and we need to verify the user can view it
-- 如果是条个人消息我们要确认用户是否有权查看
IF @SectionID = 0
BEGIN
IF NOT EXISTS (SELECT UserID FROM cs_PrivateMessages WHERE UserID = @UserID AND ThreadID = @ThreadID AND SettingsID = @SettingsID)
RETURN
/*不存在此用户的ID就返回*/
END
-- Create a temp table to store the select results
-- 如果不是个人消息
CREATE TABLE #PageIndex
(
IndexID int IDENTITY (1, 1) NOT NULL,
PostID int
)
-- Sort by Post Date
-- 按帖子日期排序
IF @SortBy = 0 AND @SortOrder = 0
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY PostDate
ELSE IF @SortBy = 0 AND @SortOrder = 1
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY PostDate DESC
-- Sort by Author
-- 按作者ID排序
IF @SortBy = 1 AND @SortOrder = 0
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY UserID
ELSE IF @SortBy = 1 AND @SortOrder = 1
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY UserID DESC
-- Sort by SortOrder
-- 按排序类型排序
IF @SortBy = 2 AND @SortOrder = 0
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY SortOrder
ELSE IF @SortBy = 2 AND @SortOrder = 1
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY SortOrder DESC
-- Select the individual posts
-- 查询个人的帖子
SELECT
P.PostID, P.ThreadID, P.ParentID, P.PostAuthor, P.UserID, P.SectionID, P.PostLevel, P.SortOrder, P.Subject, P.PostDate, P.IsApproved,
P.IsLocked, P.IsIndexed, P.TotalViews, P.Body, P.FormattedBody, P.IPAddress, P.PostType, P.EmoticonID, P.SettingsID, P.AggViews,
P.PropertyNames as PostPropertyNames, P.PropertyValues as PostPropertyValues,
P.PostConfiguration, P.UserTime, P.ApplicationPostType, P.PostName,
P.Points as PostPoints, P.RatingSum as PostRatingSum, P.TotalRatings as PostTotalRatings,
T.*, U.*, #PageIndex.*,
T.IsLocked,
T.IsSticky,
Username = P.PostAuthor,
ThreadStarterAuthor = T.PostAuthor,
ThreadStartDate = T.PostDate,
EditNotes = (SELECT EditNotes FROM cs_PostEditNotes WHERE PostID = P.PostID),
AttachmentFilename = ISNULL ( (SELECT [FileName] FROM cs_PostAttachments WHERE PostID = P.PostID), ''),
Replies = 0, --not used(SELECT COUNT(P2.PostID) FROM cs_Posts P2 (nolock) WHERE P2.ParentID = P.PostID AND P2.PostLevel != 1),
IsModerator = 0, -- not used
HasRead = 0 -- not used
FROM
cs_Posts P (nolock),
cs_Threads T,
cs_vw_Users_FullUser U,
#PageIndex
WHERE
P.PostID = #PageIndex.PostID AND
P.UserID = U.cs_UserID AND
T.ThreadID = P.ThreadID AND
#PageIndex.IndexID > @PageLowerBound AND
#PageIndex.IndexID < @PageUpperBound and U.SettingsID = @SettingsID
ORDER BY
IndexID
END
IF @ReturnRecordCount = 1
/*查询匿名状况下的总数吧,算了,卡语法吧.要懂参数还要看程序,真麻烦*/
SELECT count(PostID) FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID
(
@PostID int,
@PageIndex int,
@PageSize int,
@SortBy int,
@SortOrder bit,
@UserID int,
@ReturnRecordCount bit,
@AllowUnapproved bit = 0,
@SettingsID int
)
AS
SET Transaction Isolation Level Read UNCOMMITTED
/*设置事务隔离级别,Read UNCOMMITTED 执行脏读或 0 级隔离锁定,这表示不发出共享锁,
也不接受排它锁。当设置该选项时,可以对数据执行未提交读或脏读;在事务结束前可以更
改数据内的数值,行也可以出现在数据集中或从数据集消失。该选项的作用与在事务内所有
语句中的所有表上设置 NOLOCK 相同。这是四个隔离级别中限制最小的级别。*/
BEGIN
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
DECLARE @ThreadID int
DECLARE @SectionID int
-- First set the rowcount
-- 首先设置行数
DECLARE @RowsToReturn int
SET @RowsToReturn = @PageSize * (@PageIndex + 1)
SET ROWCOUNT @RowsToReturn
-- Set the page bounds
-- 设置也面绑定
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
-- Get the ThreadID
-- 获取主题ID
SELECT
@ThreadID = ThreadID,
@SectionID = SectionID
FROM
cs_Posts
WHERE
PostID = @PostID and SettingsID = @SettingsID
-- Is the Forum 0 (If so this is a private message and we need to verify the user can view it
-- 如果是条个人消息我们要确认用户是否有权查看
IF @SectionID = 0
BEGIN
IF NOT EXISTS (SELECT UserID FROM cs_PrivateMessages WHERE UserID = @UserID AND ThreadID = @ThreadID AND SettingsID = @SettingsID)
RETURN
/*不存在此用户的ID就返回*/
END
-- Create a temp table to store the select results
-- 如果不是个人消息
CREATE TABLE #PageIndex
(
IndexID int IDENTITY (1, 1) NOT NULL,
PostID int
)
-- Sort by Post Date
-- 按帖子日期排序
IF @SortBy = 0 AND @SortOrder = 0
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY PostDate
ELSE IF @SortBy = 0 AND @SortOrder = 1
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY PostDate DESC
-- Sort by Author
-- 按作者ID排序
IF @SortBy = 1 AND @SortOrder = 0
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY UserID
ELSE IF @SortBy = 1 AND @SortOrder = 1
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY UserID DESC
-- Sort by SortOrder
-- 按排序类型排序
IF @SortBy = 2 AND @SortOrder = 0
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY SortOrder
ELSE IF @SortBy = 2 AND @SortOrder = 1
INSERT INTO #PageIndex (PostID)
SELECT PostID FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID ORDER BY SortOrder DESC
-- Select the individual posts
-- 查询个人的帖子
SELECT
P.PostID, P.ThreadID, P.ParentID, P.PostAuthor, P.UserID, P.SectionID, P.PostLevel, P.SortOrder, P.Subject, P.PostDate, P.IsApproved,
P.IsLocked, P.IsIndexed, P.TotalViews, P.Body, P.FormattedBody, P.IPAddress, P.PostType, P.EmoticonID, P.SettingsID, P.AggViews,
P.PropertyNames as PostPropertyNames, P.PropertyValues as PostPropertyValues,
P.PostConfiguration, P.UserTime, P.ApplicationPostType, P.PostName,
P.Points as PostPoints, P.RatingSum as PostRatingSum, P.TotalRatings as PostTotalRatings,
T.*, U.*, #PageIndex.*,
T.IsLocked,
T.IsSticky,
Username = P.PostAuthor,
ThreadStarterAuthor = T.PostAuthor,
ThreadStartDate = T.PostDate,
EditNotes = (SELECT EditNotes FROM cs_PostEditNotes WHERE PostID = P.PostID),
AttachmentFilename = ISNULL ( (SELECT [FileName] FROM cs_PostAttachments WHERE PostID = P.PostID), ''),
Replies = 0, --not used(SELECT COUNT(P2.PostID) FROM cs_Posts P2 (nolock) WHERE P2.ParentID = P.PostID AND P2.PostLevel != 1),
IsModerator = 0, -- not used
HasRead = 0 -- not used
FROM
cs_Posts P (nolock),
cs_Threads T,
cs_vw_Users_FullUser U,
#PageIndex
WHERE
P.PostID = #PageIndex.PostID AND
P.UserID = U.cs_UserID AND
T.ThreadID = P.ThreadID AND
#PageIndex.IndexID > @PageLowerBound AND
#PageIndex.IndexID < @PageUpperBound and U.SettingsID = @SettingsID
ORDER BY
IndexID
END
IF @ReturnRecordCount = 1
/*查询匿名状况下的总数吧,算了,卡语法吧.要懂参数还要看程序,真麻烦*/
SELECT count(PostID) FROM cs_Posts (nolock) WHERE (IsApproved = 1 OR 1 = @AllowUnapproved) AND ThreadID = @ThreadID and SettingsID = @SettingsID