set B.counts=#temp.t(t是从什么地方来的)

解决方案 »

  1.   

    见:select userid,count(*) as t into #temp from A
    t是count(*)的别名
      

  2.   

    我想语句是正确的,因为我在查询分析器中,不论执行什么操作都正确。就是不知怎么在VB中更新时会报错,我用Err.Source看了一下,提示:Microsoft Cursor Engine。请高手帮忙解决一下,我急需!
      

  3.   

    如果有ID字段,就是具有唯一性的字段delect table where id not in (  select max(id) from table group by col1,col2,col3...
    )
    group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。3,没有ID的情况select identity(int,1,1) as id,* into #temp from tabel
    delect # where id not in (
      select max(id) from # group by col1,col2,col3...)
    delect table
    inset into table(...)
       select ..... from #temp
      

  4.   

    不好意思,敲错了1、如果有ID字段,就是具有唯一性的字段delete from table where id not in (  select max(id) from table group by col1,col2,col3...
    )
    group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。2、没有ID的情况select identity(int,1,1) as id,* into #temp from tabel
    delete from #temp where id not in (
      select max(id) from #temp group by col1,col2,col3...)
    delete table
    inset into table(...)
       select ..... from #tempdrop table #temp   
      

  5.   

    to CoolAbu(程序员跟GF不兼容(★★★★)) :
    有id,B表中的id是userid,
          A表中的是另一个字段(userid不是)
    对不起,我没搞清你的第1个建议是什么意思。
      

  6.   

    我比较笨,我不知道怎么用你给的例子,你能说明白些吗?
    我的表结构,大致如下:
    A表:
    no(主键),userid,dates,price
    B表:
    userid(主键),counts(用于统计A表中的各userid的个数)
    对了,我还少说了一句,虽然VB要报错,可它仍然更新了,不知怎么回事?
      

  7.   

    我用的一种笨方法,等待高手来优化,;)declare @A table(userid varchar(10),dates varchar(10),price varchar(10))
    insert into @A select '001','2002-12-31','51'
    insert into @A select '001','2002-12-31','51'
    insert into @A select '002','2002-12-31','52'
    insert into @A select '002','2002-12-31','52'
    insert into @A select '002','2002-12-31','53'
    insert into @A select '003','2002-12-31','53'
    declare @B table(userid varchar(10),counts int)
    insert into @B select '001',2
    insert into @B select '002',1
    insert into @B select '003',1
    select * from @Bcreate table #temp(id varchar(10),counts int)
    insert into #temp select * from @Bupdate #temp set counts=(select count(userid) from @A where userid=id)delete from @Binsert into @B select * from #temp
    drop table #tempselect * from @Buserid     counts      
    ---------- ----------- 
    001        2
    002        1
    003        1
    userid     counts      
    ---------- ----------- 
    001        2
    002        3
    003        1(所影响的行数为 3 行)
      

  8.   

    出现这样的错误是有重复的值出现了,可以尝试在触发器中开始的地方加一句。
      truncate tabel #temp.
      试试看吧。
      

  9.   

    create trigger tuser
    on A
    for  insert,delete,update
    as
    update B
    set B.countst
    from B,(select userid,count(*) t from A
    group by userid ) temp1
    where B.userid=temp1.userid
      

  10.   

    to CoolAbu(程序员跟GF不兼容(★★★★)) 
    我每次添加、更新、删除都是一条记录,临时表应该会自动删除吧?应该不会有重复记录。我现在能实现在A表中添加、更新、删除后,在B表中,能正确统计数据,只是在VB中更新则会报错,(添加则正常)而在SQL sever中一切操作都正常。
    to  xavier(尼日利亚)
    不行会出错,不信你试试,在开始的地方加,会出错,系统会提示#temp出错,我在触发器的最后加上,可仍没办法解决我问的问题。
      

  11.   

    你在VB中加一个On error goto 处理一下
      

  12.   

    先让TRIGGER 只影响UPDATE,我怀疑在VB里可能由于代码编写问题把UPDATE操作当成了INSERT,把 "for  insert,delete,update" 中"insert,delete,"去掉试试当VB里UPDATE时是否会触发
      

  13.   

    只是在VB中更新则会报错,(添加则正常)而在SQL sever中一切操作都正常。
    ........
    能否把你vb中的代码贴出来看看
      

  14.   

    to titanicliu()
    正如你所预料的,去掉insert,delete后,程序正常,请问这是怎么回事?怎么解决?