insert 触发器例子

 GO



IF EXISTS( SELECT name FROM sysobjects WHERE name='TrgChargeInsert' AND type='TR')
  DROP TRIGGER TrgChargeInsert
GO
RAISERROR ('CREATE TRIGGER: TrgChargeInsert',0,1) WITH NOWAIT
GO
CREATE TRIGGER TrgChargeInsert ON CardDealRecordConsume FOR INSERT
AS
  IF @@rowcount = 0
  RETURN
  
  SET NOCOUNT ON
  
  DECLARE @CustomerID INT, @MobilePhone VARCHAR(20), @DealAmount Money, @ArisesTime DATETIME, @BookID INT,@PhysicalCode CHAR(10),@CardID INT,@SendMessage VARCHAR(255)

  IF EXISTS(SELECT * FROM Inserted where BookID in(101,102,103,104,105))
  
  BEGIN
  SELECT @CustomerID=CustomerID,@DealAmount=DealAmount,@ArisesTime=ArisesTime,@PhysicalCode=PhysicalCode,@CardID=CardID FROM Inserted where BookID in(101,102,103,104,105)
  SELECT @MobilePhone = MobilePhone
  FROM VwCustomer WHERE PhysicalCode = @PhysicalCode
  IF @MobilePhone IS NOT NULL
  BEGIN
  SET @SendMessage = '您好!您的校园卡于 ' 
+ DATENAME(YEAR, @ArisesTime) + '年' + DATENAME(MONTH, @ArisesTime) + '月' + DATENAME(DAY, @ArisesTime) + '日 ' + DATENAME(HOUR, @ArisesTime) + '时' + DATENAME(MINUTE, @ArisesTime) + '分' + ' 充值了 ' + CAST(@DealAmount AS VARCHAR(8)) + ' 元'
   
 INSERT INTO SmsMessage(ArisesTime, PhysicalCode, CardID, CustomerID, MobilePhone, SendMessage)
  VALUES(@ArisesTime, @PhysicalCode, @CardID, @CustomerID, @MobilePhone, @SendMessage)
  END

  END
阅读更多
文章标签: insert null
个人分类: sql
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭