--测试环境
Create Table TestA (id int,Tim datetime)
insert into TestA select 1,'2005-2-1 22:00:12'
union all select 2,'2005-10-22 22:00:12'
union all select 3,'2005-10-21 22:00:12'Create Table TestB (id int,Tim datetime)--建触发器
Create Trigger T_TestA on TestA
for insert
as
declare @t datetime
select @t=tim from inserted
if datediff(hour,@t,getdate())>=2
begin
insert into TestB select id,Tim from inserted
end
else
begin
insert into TestA select id,Tim from inserted
end--测试
insert into TestA select 4,'2005-10-21 21:50:12'--查询
select * from TestAselect * from TestB--结果
id Tim
----------- ------------------------------------------------------
1 2005-02-01 22:00:12.000
2 2005-10-22 22:00:12.000
3 2005-10-21 22:00:12.000
4 2005-10-21 21:50:12.000(所影响的行数为 4 行)id Tim
----------- ------------------------------------------------------
4 2005-10-21 21:50:12.000(所影响的行数为 1 行)
Create Table TestA (id int,Tim datetime)
insert into TestA select 1,'2005-2-1 22:00:12'
union all select 2,'2005-10-22 22:00:12'
union all select 3,'2005-10-21 22:00:12'Create Table TestB (id int,Tim datetime)--建触发器
Create Trigger T_TestA on TestA
for insert
as
declare @t datetime
select @t=tim from inserted
if datediff(hour,@t,getdate())>=2
begin
insert into TestB select id,Tim from inserted
end
else
begin
insert into TestA select id,Tim from inserted
end--测试
insert into TestA select 4,'2005-10-21 21:50:12'--查询
select * from TestAselect * from TestB--结果
id Tim
----------- ------------------------------------------------------
1 2005-02-01 22:00:12.000
2 2005-10-22 22:00:12.000
3 2005-10-21 22:00:12.000
4 2005-10-21 21:50:12.000(所影响的行数为 4 行)id Tim
----------- ------------------------------------------------------
4 2005-10-21 21:50:12.000(所影响的行数为 1 行)
FOR INSERT
ASinsert into tablenameb select * from inserted where DATEDIFF ( hour ,表插入的时间 , getdate() ) 几小时数
Create Table TestA (id int,Tim datetime)
insert into TestA select 1,'2005-2-1 22:00:12'
union all select 2,'2005-10-22 22:00:12'
union all select 3,'2005-10-21 22:00:12'Create Table TestB (id int,Tim datetime)
go
--建触发器
Create Trigger T_TestA on TestA
for insert
as
insert into TestB select * from inserted where DATEDIFF ( hour ,Tim , getdate())>2
go
--测试
insert into TestA select 4,'2005-10-21 21:50:12'
select * from testa
godrop table testa
drop table testb
insert into TestA select 4,'2005-10-21 22:12:12'我测试了,也可以啊!!
但是到了今天2005-10-22 12:00:00的话,这条记录就已经插入表A 24个小时,这时我就将表A的这条记录插入到表B,,楼上说的是这样意思吗?
还没测试,不知道是不是,
楼下的继续指教。
你的命令已经提交了没机会实现,可用insert into 目标表 (Select * From 源表 where 条件)完成非触发调用