触发器 监视某列
触发器 例如我要监视
Heat(关注)列更新的时候
Heat+1 或者 Heat-1
这种情况 应该怎么写 触发器
[解决办法]
--1. 建日志表
create table tblog(objectID int,HeatTime datetime)
create trigger tri_U
on tb
after update
as
begin
if update(Heat)
begin
insert into tblog(objectID,HeatTime)
select ID,getdate() from inserted
end
end
-- 2. 查询日志表
select * from tblog
--1. 建日志表
create table tblog(objectID int,Heat int, newHeat int, HeatTime datetime)
GO
create trigger tri_U
on tb
after update
as
begin
if update(Heat)
begin
insert into tblog(objectID,Heat,newHeat,HeatTime)
select ID,D.Heat,I.Heat,getdate()
from INSERTED I
INNER JOIN DELETED D
ON I.ID = D.ID
WHERE I.Heat = d.Heat + 1 OR i.Heat = D.Heat - 1
end
end
GO
-- 2. 查询日志表
select * from tblog
create trigger tri_U
on tb
after update
as
begin
if update(Heat)
BEGIN
INSERT INTO tableA(id, heat)--支持同时插入多条记录.
select ID,D.Heat
from INSERTED I
INNER JOIN DELETED D
ON I.ID = D.ID
WHERE I.Heat = d.Heat + 1
DELETE A
FROM tableB A
INNER JOIN INSERTED I
ON a.id = i.id
INNER JOIN DELETED D
ON I.ID = D.ID
WHERE I.Heat = d.Heat - 1
end
end
GO