1.server2005 开发版 不能用after关键字
2.这样的触发器:if(exists (select name from sysobjects where name='T_Topic'))
drop trigger T_Topic
go
create trigger T_Topic
on Topic
for insert
as
declare @score int
declare @TUid int
insert into Topic values ('','',@TUid,@score)
if (exists (select UScore from users where Uscore < @score))
begin
Raiserror('您的积分不足',16,1)
Rollback transaction
end
else
update Users set UScore=UScore-@score where UId=@TUid
就不能在Topic插入数据了,错哪儿了?

解决方案 »

  1.   

    你建立的触发器就是对表Topic的插入操作,怎么在触发器中又存在对Topic 的插入操作,不会死循环吗?
      

  2.   

    create trigger T_Topic
    on Topic
    for insert
    as
    if (exists (select UScore from users a,inserted i where a.uid = b.tuid and a.Uscore < b.score))
    begin
    Raiserror('您的积分不足',16,1)
    Rollback transaction
    end
    else
    update a set a.UScore=a.UScore- i.score from Users a,inserted i where a.UId=i.TUid
      

  3.   

    if(exists (select name from sysobjects where name='T_Topic'))
    drop trigger T_Topic
    go
    create trigger T_Topic
    on Topic
    INSTEAD OF  insert
    as
    declare @score int
    declare @TUid int
    加条件判断后再插入
    insert into Topic values ('','',@TUid,@score)
    if (exists (select UScore from users where Uscore < @score))
    begin
    Raiserror('您的积分不足',16,1)
    Rollback transaction
    end
    else
    update Users set UScore=UScore-@score where UId=@TUid
      

  4.   

    if(exists (select name from sysobjects where name='T_Topic'))
    drop trigger T_Topic
    go
    create trigger T_Topic
    on Topic
    INSTEAD OF insert
    as
    declare @score int
    declare @TUid int
    加条件判断后再插入
    if (exists (select UScore from users where Uscore < @score))insert into Topic values ('','',@TUid,@score)begin
    Raiserror('您的积分不足',16,1)
    Rollback transaction
    end
    else
    update Users set UScore=UScore-@score where UId=@TUid
      

  5.   

    insert into Topic values ('','',@TUid,@score)
    if (exists (select UScore from users where Uscore < @score))
    begin
    Raiserror('您的积分不足',16,1)
    Rollback transaction其中@score 未赋值 导致后边执行了 Rollback transaction 所以会不能插入数据
    还有就是不知道
    insert into Topic values ('','',@TUid,@score)
     这句实干什么用的
      

  6.   

    总体意思是:
    在Topic表中插入输入时有TScore,User表中的Score减去相应的TScore
      

  7.   


    是在Topic插入时,减去在Users中所有的user的score。我想是是在Topic插入时,减去在Users中相应的user的score。
      

  8.   

    这个是与INSERTED关联的啊,数不对吗
      

  9.   


    这是在Topic插入时,减去在Users中在Topic有的所有的user的score;
    我想的是在Topic插入时,减去在Users中相应的user的score,不是所有的。。
      

  10.   

    关联了INSERTED表就只会减去插入的ID对应的SCORE,你是测试出来的数不对吗?
      

  11.   

    嗯,测出来的就是Users表中减去了在Topic表中所有的user的分Score