SQL Server 觸發器demo

 

GO ip

/****** Object: Trigger [dbo].[tri_device] Script Date: 2018/6/11 10:56:08 ******/ get

SET ANSI_NULLS ON date

GO select

SET QUOTED_IDENTIFIER ON im

GO db

 

Create trigger [dbo].[tri_device] vi

on [dbo].[Device] let

for insert,update,delete delete

as

begin

/*

* 插入操做

*/

if EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)

insert into RealData(DtuId,Val,RcvTime)

select DtuId,'0',getdate() from inserted;

/*

*更新操做

*/

IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)

update RealData set DtuId=a.DtuId from inserted a

/*

*刪除操做

*/

IF NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)

delete from RealData where DtuId=(select a.DtuId from deleted a)

end;

相關文章
相關標籤/搜索