我需要一个触发器,是这样的当一个表中的记录达到一定的条数时候,激活触发器删除这个表中比较旧的数据。如,表A当记录条数达到100时,我要利用触发器自动的删除前十条的数据。
    本人急需希望各位高手帮帮忙。

解决方案 »

  1.   


    create table tb(id int,startday varchar(10),zhouqi int,finishday varchar(10))
    go
    insert into tb select 1,'2008-04-23',1,null
    insert into tb select 2,'2008-04-23',7,null
    insert into tb select 3,'2008-04-24',10,null
    insert into tb select 4,'2008-04-22',9,null
    insert into tb select 5,'2008-04-23',1,null
    insert into tb select 6,'2008-04-23',7,null
    insert into tb select 7,'2008-04-24',10,null
    insert into tb select 8,'2008-04-22',9,null
    go
    select * from tb;
    gocreate trigger triggername
    on tb
    for insert
    as
    begin
       if ((select count(1) from tb ) >= 10)
             delete a from tb a where exists(select top 5 * from tb where a.id = id)
    end
    goinsert into tb select 9,'2008-04-23',1,null
    insert into tb select 10,'2008-04-23',7,null
    insert into tb select 11,'2008-04-24',10,null
    insert into tb select 12,'2008-04-22',9,null
    insert into tb select 9,'2008-04-23',1,null
    insert into tb select 10,'2008-04-23',7,null
    insert into tb select 11,'2008-04-24',10,null
    insert into tb select 12,'2008-04-22',9,null
    select * from tb;
    godrop table tb