大题目 topic下面有0~N个question,
topic有个属性scores,每一个question下面也有scores。
topic.scores = sum(question.scores)
请问如何保证这一点:topic.scores = sum(question.scores)?

解决方案 »

  1.   

    topic.scores应该是computed字段吧.
      

  2.   

     不知道行不? 
     create trigger tig_question on question
     for insert,update,delete
     as
      begin
           if exists(select 1 from inserted) --添加或更新
          begin
        update topic set topic.scores=(select isnull(sum(scores),0) from question,inserted where question.topicid=inserted.topicid) from inserted where inserted.topicid=topic.topicid
         end
         if exists(select 1 from deleted)--删除情况
           begin
     update topic set topic.scores=(select isnull(sum(scores),0) from question,deleted where question.topicid=deleted.topicid) from deleted where deleted.topicid=topic.topicid
           end
      end