想做一个insert事件触发器 在表 answer里 多项选择题的答案     字段4
                                    a,b,c 
                                    a,b,d 
例如把a,b,c       更新 表xx  
               字段2   字段3
                a        1
                b        1
                c        1
                d        0使得字段3里的相对应项  自动加1

解决方案 »

  1.   

    create trigger ti_answer on answer
    for insert
    as
    declare @str varchar(100)
    declare cur_tmp cursor for
    select 字段4 from inserted
    open cur_tmp
    fetch next from cur_tmp into @str
    while @@fetch_status=0
    begin
    update xx
    set 字段3=isnull(字段3,0)+1
    from xx
    where charindex(','+字段2+',',','+@str+',')>0
    fetch next from cur_tmp into @str
    end
    close cur_tmp
    deallocate cur_tmp
      

  2.   

    在表 answer里 多项选择题的答案     answer
                                        a,b,c 
                                        a,b,d 
    例如把a,b,c       更新 表xxb  
                    xx       cs
                    a        1
                    b        1
                    c        1
                    d        0使得字段3里的相对应项  自动加1语句是么问题 但按确定后有点小问题 我把里面的字段2改成了xx  字段3改成了 cs 字段4改成 answer
    在answer表里添加数据的时候显示错误
      
      名字为‘cur_tmp’的游标不存在
      语句已终止为什么
      

  3.   

    drop table answer,xxb
    go
    create table answer(answer varchar(100))create table xxb(xx varchar(10),cs int)
    insert into xxb
    select 'a',1
    union all select 'b',1
    union all select 'c',1
    union all select 'd',0go
    create trigger ti_answer on answer
    for insert
    as
    declare @str varchar(100)
    declare cur_tmp cursor for
    select answer from inserted
    open cur_tmp
    fetch next from cur_tmp into @str
    while @@fetch_status=0
    begin
    update xxb
    set cs=isnull(cs,0)+1
    from xxb
    where charindex(','+xx+',',','+@str+',')>0
    fetch next from cur_tmp into @str
    end
    close cur_tmp
    deallocate cur_tmp
    go
    insert into answer
    select 'a,b,c'
    union all select 'a,b,d'select * from xxb/*
    xx         cs          
    ---------- ----------- 
    a          3
    b          3
    c          2
    d          1(所影响的行数为 4 行)*/