表tab_user
id   name    say
1   张三   你好
2 李四   你也好
3 张三   dfdfdf
4   王五    dfdfdfd
5   李四      fdfdfdfd表:tab_count
id   name      count
tab_user表里的记录是随时在增加的
统计name字段同一名称(比如所有为"张三"的)的记录总数写入到tab_count表里求这样的触发器,不明白的发言!
谢谢!50分送上,谢谢!分不多了,名们兄弟帮忙!

解决方案 »

  1.   

    直接写不就行了吗delete from tab_count
    insert into tab_count(name,count) values
           select name,count(*) from tab_user
           group by name
      

  2.   

    CREATE TRIGGER triDefer ON dbo.tab_user 
    FOR INSERT
    AS
    Declare @id  as varchar(10)
    Declare @name  as varchar(10)
    select @id=id from inserted
    select @name = name from inserted
    if IF exists(select * from tab_count where id = @id)
    begin
    update tab_count set tab_count.count=(select count(*) from tab_user where id=@id)
    where id=@id
    End
    else
    begin
    INSERT INTO tab_count(id,name,count) value (@id,@name,1)
    end
      

  3.   

    写的急,可能会有点错误,这个只在insert时执行,如果需要在update时也执行就需要用游标来控制
      

  4.   

    to xiaoyi1981(xiaoyi)
    如果批量插入你的过程就会出错,可惜结贴了