create trigger t_time on 表1
for insert
as
begin
insert into 表2 (TIMESPAN)
select datediff(hour,a.CALTIME,b.CALTIME) --如果表2中需要纪录日期,则在这里可以加上一列 convert(varchar(10),a.CALTIME,120)
from 表1 a inner join inserted b
on a.SWITCH = '开' and b.SWITCH = '关'
and convert(varchar(10),a.CALTIME,120) = convert(varchar(10),b.CALTIME,120)
end
go
create trigger t_time on 表1
for insert
as
begin
insert into 表2 (TIMESPAN)
select datediff(hour,(select MAX(CALTIME) from 表1 WHERE SWITCH = '开'),inserted.CALTIME)
WHERE inserted.SWITCH = '关'
end
go
create trigger trgtest on tablename1
instead of insert
as
begin
insert into tablename2(timespan)
select datediff(hour,b.caltime,a.caltime)
from inserted a ,
(select max(caltime) as caltime from tablename1) b
where a.switch='开'
insert into tablename1 select * from inserted
end
create trigger t_time on 表1
after insert
as
begin
declare @switch char(5)
declare @mtime datetime
select @switch=switch from 表1 where CALTIME=(select Max(CALTIME) from 表1)
if @switch='关'
insert into 表2(timespan)
select datediff(hour,caltime,caltime)
from 表1
end
go