--测试环境
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 行)

解决方案 »

  1.   

    CREATE TRIGGER [TRIGGERNAME] ON [dbo].[tablename] 
    FOR INSERT
    ASinsert into tablenameb select * from inserted where DATEDIFF ( hour ,表插入的时间  , getdate() ) 几小时数
      

  2.   

    借用上面的测试数据,上面的如果一次插入多条就不好用了
    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
      

  3.   

    --插入多条数据是指什么??insert into TestA select 4,'2005-10-21 22:11:12'
    insert into TestA select 4,'2005-10-21 22:12:12'我测试了,也可以啊!!
      

  4.   

    我是说本来我表A中前天就查入一条数据,比如时间是2005-10-21 12:00:00
    但是到了今天2005-10-22 12:00:00的话,这条记录就已经插入表A 24个小时,这时我就将表A的这条记录插入到表B,,楼上说的是这样意思吗?
    还没测试,不知道是不是,
    楼下的继续指教。
      

  5.   

    dongdong715:你这是从一张表备份到另一张表,不是插入啊,是你自己没说清吧
      

  6.   

    触发器是针对SQL DML 命令执行的时候进行对应处理的机制
    你的命令已经提交了没机会实现,可用insert into 目标表 (Select * From 源表 where 条件)完成非触发调用