begin transaction
  insert into b select 'user account',* from a where groupid=@groupid
  delete a where groupid=@groupid
  if @@rowcount=0
    begin
     rollback
     raiserror(16,1,'并发错误')
    end
 commit transaction用触发器并不能将账号传入,用个这样的事务挺好.
除非对于b没有其他的数据插入,可以使用b表的插入触发器
create trigger trgtest on b
for insert
as
begin
  delete a from a join inserted b on a.groupid=b.groupid
  if @rowcount=0
    begin
      rollback
      raiserror(16,1,'并发错误')
    end
end

解决方案 »

  1.   


    茅塞顿开~~~呵呵  强人
    组号是顺序生成的~  用户点击按钮时并不知道组号,换句话说就是:用户1 点击按钮 选取了第一组,用户2随后点击按钮 他选取的就是第二组
    依此类推。
    像朋友上面的写法,必需是groupid作为参数,如果是groupid参数未知的情况下怎么做到?
      

  2.   

    估计你要的不光是触发器吧程序不可以吗? 调用PROC
      

  3.   

    --加个取组号程序,这样就user account一个参数了
    declare @groupid int
    begin transaction 
      select top 1 @groupid=groupid from a with (xlock) order by groupid     --加锁防止冲突
      if @@error<>0
        begin 
        rollback 
        raiserror(16,1,'并发错误') 
        end   insert into b select 'user account',* from a where groupid=@groupid 
      if @@error<>0
        begin 
        rollback 
        raiserror(16,1,'并发错误') 
        end   delete a where groupid=@groupid   if @@error<>0
        begin 
        rollback 
        raiserror(16,1,'并发错误') 
        end 
    commit transaction 
      

  4.   

    请问在前台读取到错误是用@@error的值吗? 如果总是出现“并发错误”只类的提示,而没有处理过程,这可不太友好了~
      

  5.   


    就比如说 出错后出现错误页面然后自动history.go(-1)到上一页面.这样的话就必需要取得错误号了啊
    通过错误号我才能设计错误响应吧~~请赐教
      

  6.   


    create proc test
    @errorcode int output
    as
    --if an  error occurs then
    set @errorcode=@@error
    godeclare @errorcode int
    exec test @errorcode output
    select @errorcodedrop proc test