zoukankan      html  css  js  c++  java
  • cs_guestbook_GetPosts///cs_guestbook_Post_Create

    ALTER    PROCEDURE [dbo].cs_guestbook_Post_Create
    (
        
    @SectionID int,
        
    @Subject nvarchar(256),
        
    @UserID int,
        
    @PostAuthor nvarchar(64= null,
        
    @Body ntext,
        
    @FormattedBody ntext,
        
    @EmoticonID int = 0,
        
    @PostType int = 0,
        
    @PostDate datetime = null,
        
    @UserHostAddress nvarchar(32),
        
    @PropertyNames ntext = null,
        
    @PropertyValues ntext = null,
        
    @SettingsID int,
        
    @PostID int out

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


    BEGIN
    SET NOCOUNT ON
    /*当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。
    当 SET NOCOUNT 为 OFF 时,返回计数。即使当 SET NOCOUNT 为 ON 时,也更新 
    @@ROWCOUNT 函数。
    */


    DECLARE @ThreadID int

    -- set the PostDate
    --
     设置回帖日期
    IF @PostDate IS NULL
        
    SET @PostDate = GetDate()

    -- set the username
    --
     设置用户名即作者
    IF @PostAuthor IS NULL
        
    SELECT 
            
    @PostAuthor = UserName
        
    FROM 
            cs_vw_Users_FullUser 
        
    WHERE 
            cs_UserID 
    = @UserID

    -- Do we care about duplicates?
    --
     我们是否考虑完全相同的

    Select @ThreadID = ThreadID FROM cs_Threads Where SectionID = @SectionID


    SET NOCOUNT ON
    /*当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。
    当 SET NOCOUNT 为 OFF 时,返回计数。即使当 SET NOCOUNT 为 ON 时,也更新 
    @@ROWCOUNT 函数。
    */


    BEGIN TRAN -- 开始事务


    IF @ThreadID is null
    BEGIN


        
    INSERT cs_Threads     -- 插入数据
        ( 
            SectionID,
            PostDate, 
            UserID, 
            PostAuthor, 
            ThreadDate, 
            MostRecentPostAuthor, 
            MostRecentPostAuthorID,     
            MostRecentPostID, 
            IsLocked, 
            IsApproved,
            IsSticky, 
            StickyDate, 
            ThreadEmoticonID,
            SettingsID 
        )
        
    VALUES
        ( 
            
    @SectionID
            
    @PostDate
            
    @UserID
            
    @PostAuthor,
            
    @PostDate,
            
    @PostAuthor,
            
    @UserID
            
    0,    -- MostRecentPostID, which we don't know until after post INSERT below.
            0,
            
    1,
            
    0,
            
    @PostDate,
            
    -1,
            
    @SettingsID 
        )

        
    -- Get the new ThreadID
            -- 获取主题ID
            SELECT 
            
    @ThreadID = @@IDENTITY
        
    FROM
            cs_Threads
            
        
    -- Now we add the new post
            -- 现在插入新的帖子
               /*在主题表中插入数据的时候也要在帖子表中插入数据,因为主题只是帖子的信息统计
                  具体的内容还是帖子里的.
    */

        
    INSERT cs_Posts 
            ( SectionID, 
            ThreadID, 
            ParentID, 
            PostLevel, 
            SortOrder, 

            Subject, 
            UserID, 
            PostAuthor, 
            IsApproved, 
            IsLocked, 
            Body, 
            FormattedBody, 
            PostType, 
            PostDate, 
            IPAddress, 
            EmoticonID,
            PropertyNames,
            PropertyValues,
            SettingsID
                )
        
    VALUES 
            ( 
    @SectionID
            
    @ThreadID
            
    0,     -- ParentID, which we don't know until after INSERT
            1,     -- PostLevel, 1 marks start/top/first post in thread.
            1,     -- SortOrder (not in use at this time)
            @Subject
            
    @UserID
            
    @PostAuthor,
            
    1
            
    0
            
    @Body
            
    @FormattedBody
            
    @PostType
            
    @PostDate
            
    @UserHostAddress
            
    -1,
            
    @PropertyNames,
            
    @PropertyValues,
            
    @SettingsID )
            

        
    -- Get the new PostID
            -- 获取帖子ID
        SELECT 
            
    @PostID = @@IDENTITY

        
    -- Update the new Thread with the new PostID
            -- 更新主题表
        UPDATE 
            cs_Threads
        
    SET 
            MostRecentPostID 
    = @PostID
        
    WHERE 
            ThreadID 
    = @ThreadID

    END
    ELSE BEGIN    -- @ParentID <> 0 means there is a reply to an existing post
                    -- 如果@ParentID不为0则表示有回复
        -- Insert the new post
        INSERT cs_Posts 
            ( SectionID, 
            ThreadID, 
            ParentID, 
            PostLevel, 
            SortOrder, 
            Subject, 
            UserID, 
            PostAuthor, 
            IsApproved, 
            IsLocked, 
            Body, 
            FormattedBody, 
            PostType, 
            PostDate, 
            IPAddress, 
            EmoticonID,
            PropertyNames,
            PropertyValues,
            SettingsID )
        
    VALUES 
            ( 
    @SectionID
            
    @ThreadID
            
    0
            
    1
            
    0,
            
    @Subject
            
    @UserID
            
    @PostAuthor
            
    1
            
    0
            
    @Body
            
    @FormattedBody
            
    @PostType
            
    @PostDate
            
    @UserHostAddress
            
    -1,
            
    @PropertyNames,
            
    @PropertyValues,
            
    @SettingsID  )


            
    -- Grab the new PostID and update the ThreadID's info
                    -- 获取新的回帖ID并更新主题表
            SELECT 
                
    @PostID = @@IDENTITY 




            
    UPDATE
                cs_Threads     
            
    SET 
                TotalReplies 
    = (SELECT COUNT(*FROM cs_Posts WHERE ThreadID = @ThreadID)
            
    WHERE
                ThreadID 
    = @ThreadID
        
    END
    END


    COMMIT TRAN

    SET NOCOUNT OFF


    SELECT @PostID = @PostID
    cs_guestbook_GetPosts
  • 相关阅读:
    Microsoft Enterprise Library 5.0 系列(二) Cryptography Application Block (初级)
    Microsoft Enterprise Library 5.0 系列(五) Data Access Application Block
    Microsoft Enterprise Library 5.0 系列(八) Unity Dependency Injection and Interception
    Microsoft Enterprise Library 5.0 系列(九) Policy Injection Application Block
    Microsoft Enterprise Library 5.0 系列(三) Validation Application Block (高级)
    软件研发打油诗祝大家节日快乐
    从挖井的故事中想到开发管理中最容易忽视的几个简单道理
    ITIL管理思想的执行工具发布
    管理类软件设计“渔”之演化
    20070926日下午工作流与ITILQQ群 事件管理 讨论聊天记录
  • 原文地址:https://www.cnblogs.com/ruanbl/p/509744.html
Copyright © 2011-2022 走看看