团队触发器

USE [MogoData355]
GO
/****** 对象:  Trigger [dbo].[UserWithdrawOrderStatus]    脚本日期: 07/05/2013 14:17:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[UserWithdrawOrderStatus] 
   ON  [dbo].[User_Withdraw_Detail] 
   AFTER UPDATE
AS 
BEGIN    
    SET NOCOUNT ON;
    DECLARE @Count int;
    DECLARE @FinishCount int;
    DECLARE @FailCount int;
    DECLARE @Woid int;
   if (update(Status))
       BEGIN
       SELECT @Woid = WOID from inserted;
       select @Count =count(*) from User_Withdraw_Detail where woid = @Woid ;
       select @FinishCount=count(*) from User_Withdraw_Detail where woid =  @Woid and (Status =3 or Status =4) ;
       select @FailCount=count(*) from User_Withdraw_Detail where woid =  @Woid and Status =4;
        if (@Count = @FinishCount)
        BEGIN
                if (@FailCount > 0)
                BEGIN
                    update User_Withdraw_Order set Status = 4 where ID = @Woid
                END
                else
                BEGIN
                    update User_Withdraw_Order set Status = 3 where ID = @Woid
                END
        END

--        if (@FailCount > 0)
--            BEGIN
--                update User_Withdraw_Order set Status = 4 where ID = @Woid
--            END
  END
END   
--------------------------------------------------------------
USE [MogoData355]
GO
/****** 对象:  Trigger [dbo].[UserWithdrawDetailSuccess]    脚本日期: 07/05/2013 14:17:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER TRIGGER [dbo].[UserWithdrawDetailSuccess] 
   ON  [dbo].[User_Withdraw_Detail] 
   AFTER UPDATE
AS 
BEGIN    
    SET NOCOUNT ON;
    DECLARE @Status int;
    DECLARE @UID uniqueidentifier;
    DECLARE @Amount money;
    DECLARE @NoteNum varchar(20);
    DECLARE @WOID int;
    DECLARE @ID int
   if (update(Status))
   BEGIN
   SELECT @UID=UID, @Status=Status,@Amount=Amount,@NoteNum =WOID+'0'+ID, @ID=ID,@WOID =WOID from inserted 
    if (@Status=3)
      BEGIN
        --UPDATE User_Income SET Withdraw=(select sum(Amount) as Amount from User_Withdraw 
        --where UID=@UID and Status=3)*1000 where UID=@UID
        INSERT INTO User_Income_Detail(UID,Date,Type,Amount,Note,Summed)
        VALUES (@UID,CONVERT(VARCHAR(10),GETDATE(),120),-1,-@Amount,@NoteNum,1)
      END
     if (@Status=4)
      BEGIN
        --UPDATE User_Income SET Withdraw=(select sum(Amount) as Amount from User_Withdraw 
        --where UID=@UID and Status=3)*1000 where UID=@UID
        update  User_Withdraw_Detail set Tax =0,RealityAmount=0 where ID = @ID and WOID = @WOID
      END
   END   
END


---------------------------------------------------------------------
USE [MogoData355]
GO
/****** 对象:  Trigger [dbo].[UserWithdrawOrderRealityAmoun]    脚本日期: 07/05/2013 14:19:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[UserWithdrawOrderRealityAmoun] 
   ON  [dbo].[User_Withdraw_Order] 
   AFTER UPDATE
AS 
BEGIN    
    SET NOCOUNT ON;
    DECLARE @Status tinyint;
    DECLARE @WOID int;
   if (update(Status))
       BEGIN
         SELECT @Status = [Status], @WOID = ID from inserted;
         if(@Status = 3 or @Status = 4)
            BEGIN
                update User_Withdraw_Order set RealityAmount =
                (select isnull(sum(RealityAmount),0.0) from User_Withdraw_Detail where WOID = @WOID and Status=3),
                Tax =
                (select isnull (sum(Tax),0.0) from User_Withdraw_Detail where WOID = @WOID and Status=3)
                where ID = @WOID
            END
       END
END   
原文地址:https://www.cnblogs.com/l25321937/p/3191080.html