zoukankan      html  css  js  c++  java
  • 自己写的sql server触发器练练--高手请您跳过吧

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go

    ALTER TRIGGER [insertReplyToic]
    ON [dbo].[bbsReplyTopic]
    AFTER insert
    AS
    BEGIN
    --SET NOCOUNT ON;

    -- Insert statements for trigger here

    declare @uid int,@topicId int,@Rcontent nvarchar(max),@Rtime datetime,@ccode varchar(max)
    ,@uname nvarchar(50),@beReplyedUid int,@beReplyedUname nvarchar(50),@replyTid int,@insertId int;
    insert into bbsReplyTopic values(
    @uid,@topicId,@Rcontent,@Rtime,@ccode,@uname,@beReplyedUid,@beReplyedUname,@replyTid
    ) select @insertId=@@identity
    if(@insertId!=0)

    select @topicId=topicId from inserted
    update bbsTopic set replyCount=replyCount+1 where topicId=@topicId
    print '您刚刚插入的id是'+convert(varchar(10),@insertId)+',success'

    END

    insert into bbsReplyTopic values(125,291,'test','2014/10/6 00:00:00','chadao','zhangziyi',125,'zhangziyi',0)

    自己写的sql server触发器,当回帖帖子被回复时,帖子的回复数量字段就加1

    测试成功!

    后来,我自己想了想,如果我要实现我插入一条数据的话,与这个表关联的另一个表也相应地更新一下,那么我该怎么做,后来参考下资料,发觉如果我照上面去做的话,是实现不了的,所以我把上面的改了,如下:

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go


    ALTER TRIGGER [insertReplyToic]
    ON [dbo].[bbsReplyTopic]
    AFTER insert
    AS
    BEGIN

    declare @uid int,@topicId int,@Rcontent nvarchar(max),@Rtime datetime,@ccode varchar(max)
    ,@uname nvarchar(50),@beReplyedUid int,@beReplyedUname nvarchar(50),@replyTid int,@insertId int;

    select @insertId=@@identity
    select @topicId=topicId from inserted
    update bbsTopic set replyCount=replyCount+1 where topicId=@topicId
    print '您刚刚插入的id是'+convert(varchar(10),@insertId)+',success'

    END

     这样,也就实现我要的功能,嘿嘿!

    本人博客的文章若有侵犯他人的地方,请告知!若有写的不对的地方,请指正!谢谢!
  • 相关阅读:
    PAT2019顶级7-2:美丽的序列(线段树+DP)
    ZOJ2112 Dynamic Rank(可持久化线段树套树状数组)
    CF1353E K-periodic Garland(动态规划)
    CF1353D Constructing the array(优先队列)
    HDU2069 Coin Change(基础DP)
    surf(树状数组+DP)
    双倍快乐(回文树)
    ZOJ3591 Nim(博弈论)
    HDU6601 Keep On EveryThing But Triangle(可持久化线段树)
    HDU6599 I Love Palindrome String(回文树)
  • 原文地址:https://www.cnblogs.com/QMM2008/p/4008275.html
Copyright © 2011-2022 走看看