sqlserver触发器实例

        下面是我自己写的一个sqlserver的触发器,用于在保存一条记录后把这条记录在保存到另外一张表,特此记录一下。
 
--根据DynData1表创建DynDataDump
SELECT * INTO DynDataDump FROM DynData1;
--测试表结构
--SELECT * FROM DynDataDump
--删除触发器
DROP TRIGGER updateDynDataDump1
--创建触发器
CREATE TRIGGER updateDynDataDump1
ON DynData1
AFTER INSERT
AS
DECLARE @count tinyint,
  @addip varchar(20)  
SELECT @addip=ins.addressip FROM inserted as ins
SELECT @count=COUNT(addressip) FROM DynDataDump WHERE addressip=@addip
IF @count>0
BEGIN
 DELETE FROM DynDataDump WHERE addressip=@addip
 INSERT INTO DynDataDump SELECT * FROM inserted
END
ELSE
BEGIN
 INSERT INTO DynDataDump SELECT * FROM inserted
END
GO
--插入测试
INSERT DynData1
(addressip,time,longitude,latitude,velocity,angle,alarm,light,oil,locate,taxistate)
VALUES
('123455',getdate(),123.45644,45.62347,1,1,1,0,0,1,0);
--查询结果
SELECT * FROM DynDataDump;

你可能感兴趣的:(触发器,数据库,职场,sqlserver,休闲)