日期:2014-05-18 浏览次数:20920 次
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author:        brallow
-- Create date: 2008-10-17
-- Description:    Trigger Example For CSDN
-- =============================================
CREATE TRIGGER  [QUAutoCalc]
   ON  [dbo].[exam_QuestionName]
   AFTER INSERT,DELETE,UPDATE
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    Declare @eQuestionCode as varchar(10);
        Declare @eTypeCode as varchar(10);
    Declare @eQuestionNum as int;  
    --处理删除的数据
    select 
    @eQuestionCode= QuestionCode,
    @eTypeCode = TypeCode,
    @eQuestionNum=QuestionNum 
    from Deleted;
    if(@eQuestionCode is not Null and @eTypeCode is not Null)
        begin
        update exam_QuestionType
        set QuestionNum = QuestionNum - @eQuestionNum
        where TypeCode = @eTypeCode;
    end
    --清空数据
    set @eQuestionCode = Null;
    set @eTypeCode = Null;
    set @eQuestionNum = Null;
    --处理插入的数据    
    select 
    @eQuestionCode= QuestionCode,
    @eTypeCode = TypeCode,
    @eQuestionNum=QuestionNum 
    from Inserted;
    if(@eQuestionCode is not Null and @eTypeCode is not Null)
        begin
        update exam_QuestionType
        set QuestionNum = QuestionNum + @eQuestionNum
        where TypeCode = @eTypeCode;
    end
END