cs_forums_Post

本文介绍了一个用于查询论坛单个帖子基本信息的SQL存储过程。该过程不仅能够获取帖子的详细信息,还支持更新帖子的浏览计数、标记帖子为已读等功能。
ContractedBlock.gifExpandedBlockStart.gifcs_forums_Post
None.gifALTER  PROCEDURE dbo.cs_forums_Post
None.gif(
None.gif    
@PostID int,
None.gif    
@UserID int,
None.gif    
@TrackViews bit,
None.gif    
@SettingsID int,
None.gif    
@MarkRead bit = 0
None.gif
None.gif
AS
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif    Procedure for getting basic information on a single post.
InBlock.gif       获取单个帖子的基本信息
ExpandedBlockEnd.gif
*/

None.gif
SET Transaction Isolation Level Read UNCOMMITTED --改变事务的隔离级
ExpandedBlockStart.gifContractedBlock.gif
 /**//* Read UNCOMMITTED  执行脏读或 0 级隔离锁定,这表示不发出共享锁,也不接受排它锁。
InBlock.gif   当设置该选项时,可以对数据执行未提交读或脏读;在事务结束前可以更改数据内的数值,
InBlock.gif  行也可以出现在数据集中或从数据集消失。该选项的作用与在事务内所有语句中的所有表上
ExpandedBlockEnd.gif  设置 NOLOCK 相同。这是四个隔离级别中限制最小的级别。
*/

None.gif
None.gif
DECLARE @NextThreadID int
None.gif
DECLARE @PrevThreadID int
None.gif
DECLARE @ThreadID int 
None.gif
DECLARE @SectionID int
None.gif
DECLARE @SortOrder int
None.gif
DECLARE @IsApproved bit
None.gif
DECLARE @IsAnonymousUser bit
None.gif
None.gif
SELECT 
None.gif    
@ThreadID = ThreadID, 
None.gif    
@SectionID = SectionID, 
None.gif    
@SortOrder=SortOrder,
None.gif    
@IsApproved = IsApproved
None.gif
FROM 
None.gif    cs_Posts (nolock) 
ExpandedBlockStart.gifContractedBlock.gif      
/**//*不要发出共享锁,并且不要提供排它锁。当此选项生效时,
InBlock.gif       可能会读取未提交的事务或一组在读取中间回滚的页面。有可能发生脏读。
ExpandedBlockEnd.gif       仅应用于 SELECT 语句
*/

None.gif
WHERE 
None.gif    PostID 
= @PostID and SettingsID = @SettingsID
ExpandedBlockStart.gifContractedBlock.gif       
/**//*查询此回帖的主题ID,版块ID,排序类型,是否核准*/     
None.gif 
None.gif
-- Do we have an anonymous user?
None.gif--
判断是否是匿名用户,设置@IsAnonymousUser
None.gif
SET @IsAnonymousUser = (SELECT IsAnonymous FROM cs_vw_Users_FullUser WHERE cs_UserID = @UserID and SettingsID = @SettingsID)
ExpandedBlockStart.gifContractedBlock.gif        
/**//*系统有几个视图保持着挺多信息的,最好也看下,不过我还没看*/
None.gif
None.gif
None.gif
-- Is the Forum 0 (If so this is a private message and we need to verify the user can view it
None.gif--
(如果是条个人消息我们要验证此用户是否有权查看)
None.gif
IF @SectionID = 0
None.gif
BEGIN
None.gif    
IF NOT EXISTS (SELECT UserID FROM cs_PrivateMessages WHERE UserID = @UserID AND ThreadID = @ThreadID)
ExpandedBlockStart.gifContractedBlock.gif           
/**//*如果不存在此消息,返回*/
None.gif        
RETURN
None.gif
END
None.gif
None.gif
DECLARE @TrackingThread bit
None.gif
None.gif
IF @TrackViews = 1 
None.gif
BEGIN  --如果跟踪查看为1,更新帖子被浏览的次数
None.gif
    -- Update the counter for the number of times this post is viewed
None.gif
    UPDATE cs_Posts SET TotalViews = (TotalViews + 1WHERE PostID = @PostID and SettingsID = @SettingsID
ExpandedBlockStart.gifContractedBlock.gif        
/**//*更新帖子表*/
None.gif    
UPDATE cs_Threads SET TotalViews = (TotalViews + 1WHERE ThreadID = @ThreadID and SettingsID = @SettingsID
ExpandedBlockStart.gifContractedBlock.gif        
/**//*更新主题表*/
None.gif
END
None.gif
None.gif
-- Mark the post as read if this user is not anonymous
None.gif--
如果用户不是匿名设置帖子可读
None.gif
IF @MarkRead = 1 AND @IsAnonymousUser = 0 AND @IsApproved = 1
None.gif
BEGIN
None.gif    
IF NOT EXISTS (SELECT ThreadID FROM cs_ThreadsRead WHERE ThreadID = @ThreadID AND UserID = @UserID and SettingsID = @SettingsID and SectionID = @SectionID)
ExpandedBlockStart.gifContractedBlock.gif              
/**//*如果在读主题表中不存在此记录则插入新记录*/
None.gif        
INSERT INTO cs_ThreadsRead (UserID, ThreadID, SectionID, SettingsID) VALUES (@UserID@ThreadID@SectionID@SettingsID)
None.gif
END
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//* 这边被注释了,好象是我们的头注释的
InBlock.gif-- get the anonymous user id for this site
InBlock.gifif( @UserID = 0 ) 
InBlock.gifBEGIN
InBlock.gif    exec cs_GetAnonymousUserID @SettingsID, @UserID output
InBlock.gifEND
InBlock.gif
InBlock.gif
InBlock.gifIF EXISTS(SELECT ThreadID FROM cs_TrackedThreads (nolock) WHERE ThreadID = @ThreadID AND UserID=@UserID)
InBlock.gif    SELECT @TrackingThread = 1
InBlock.gifELSE
InBlock.gif    SELECT @TrackingThread = 0
ExpandedBlockEnd.gif
*/

None.gif
None.gif
-- Get tracking thread information
None.gif--
获取跟踪主题信息
None.gif
IF (@IsAnonymousUser = 0AND EXISTS(SELECT ThreadID FROM cs_TrackedThreads WHERE ThreadID = @ThreadID AND UserID=@UserID)
ExpandedBlockStart.gifContractedBlock.gif        
/**//*如果不是匿名用户且存在此主题记录*/
None.gif    
SET @TrackingThread = 1
None.gif
ELSE
None.gif    
SET @TrackingThread = 0
None.gif
None.gif
SELECT
None.gif    P.PostID, P.ThreadID, P.ParentID, P.PostAuthor, P.UserID, P.SectionID, P.PostLevel, P.SortOrder, P.Subject, P.PostDate, P.IsApproved,
None.gif    P.IsLocked, P.IsIndexed, P.TotalViews, P.Body, P.FormattedBody, P.IPAddress, P.PostType, P.EmoticonID, P.SettingsID, P.AggViews,
None.gif    P.PostPropertyNames 
as PostPropertyNames, P.PostPropertyValues as PostPropertyValues,
None.gif    P.PostConfiguration,P.UserTime, P.ApplicationPostType, P.PostName,
None.gif    P.Points 
as PostPoints, P.RatingSum as PostRatingSum, P.TotalRatings as PostTotalRatings,
None.gif    U.
*, P.PostAuthor as [Username],
None.gif    T.ThreadDate,
None.gif    T.StickyDate,
None.gif    T.IsLocked,
None.gif    T.IsSticky,
None.gif    T.RatingSum,
None.gif    T.TotalRatings,
None.gif    HasRead 
= 0,
None.gif    EditNotes 
= (SELECT EditNotes FROM cs_PostEditNotes WHERE PostID = P.PostID),
None.gif    IndexInThread 
= (SELECT Count(PostID) FROM cs_Posts P1 WHERE IsApproved = 1 AND ThreadID = @ThreadID AND SortOrder <= (SELECT SortOrder FROM cs_Posts where PostID = @PostID)),
None.gif    AttachmentFilename,ContentType, IsRemote, FriendlyFileName, ContentSize, 
[FileName],p.Created, Height, Width,
None.gif
--    AttachmentFilename = ISNULL ( (SELECT [FileName] FROM cs_PostAttachments WHERE PostID = P.PostID), ''),
None.gif
    IsModerator = (SELECT Count(*FROM cs_Moderators WHERE UserID = U.cs_UserID),
None.gif    Replies 
= (SELECT COUNT(*FROM cs_Posts P2 (nolock) WHERE P2.ParentID = P.PostID AND P2.PostLevel != 1),
None.gif    PrevThreadID 
= 0,
None.gif    NextThreadID 
= 0,
None.gif    UserIsTrackingThread 
= @TrackingThread
None.gif
FROM 
None.gif    cs_vw_PostsWithAttachmentDetails P,
None.gif    
--cs_Posts P,
None.gif
    cs_Threads T,
None.gif    cs_vw_Users_FullUser U
None.gif
WHERE 
None.gif    P.PostID 
= @PostID AND
None.gif    P.ThreadID 
= T.ThreadID AND
None.gif    P.UserID 
= U.cs_UserID and P.SettingsID = @SettingsID and U.SettingsID = @SettingsID
ExpandedBlockStart.gifContractedBlock.gif          
/**//*又是从视图里找,而且找好多字段,因为视图保存了好象字段,非看不可了*/
None.gif

转载于:https://www.cnblogs.com/ruanbl/archive/2006/09/20/509720.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值