+ -
当前位置:首页 → 问答吧 → 帮忙看个事务 问题

帮忙看个事务 问题

时间:2011-12-27

来源:互联网

插入重复数据不回滚继续执行Update,是什么造成的

C# code

sql+=@"BEGIN TRANSACTION
            begin
                 if not exists(select * from B_ArchiveList where relationInfo=@relation and status='true') 
                 begin
                    Insert into B_ArchiveList(archiveName,relationInfo,bolNecessary,createDate,modifyDate) values(@name,@relation,@necessary,@createDate,@modifyDate)
                 end
                
                if @@error<>0 
                begin
                    ROLLBACK TRANSACTION 
                    return
                end
            end            
            begin 
                ";
                for(int i=0;i<list.Count;i++)
                {
                    sql+=@" update T_PersonInfo set stateFlag=@stateFlag"+i+" where stateFlag=@state"+i;
                    }
                sql+=@"
                    if @@error<>0 
                    begin    
                        ROLLBACK TRANSACTION return     
                    end 
                 end 

                COMMIT TRANSACTION            
                ";

作者: shwicho   发布时间: 2011-12-27

你给表字段需要唯一的地方加个唯一约束

作者: ssp2009   发布时间: 2011-12-27

已经判断了,就不会插入错误了,怎么会回滚呢?

作者: qianjin036a   发布时间: 2011-12-27

有判断,是不是看错了,根本不会重复

作者: Haiwer   发布时间: 2011-12-27

sql+=@"BEGIN TRANSACTION
  begin
  if not exists(select * from B_ArchiveList where relationInfo=@relation and status='true') 
  begin
  Insert into B_ArchiveList(archiveName,relationInfo,bolNecessary,createDate,modifyDate) values(@name,@relation,@necessary,@createDate,@modifyDate)
   
  if @@error<>0 
  begin
  ROLLBACK TRANSACTION 
  return
  end
  end

  end 
else  
   
  begin 
  ";
  for(int i=0;i<list.Count;i++)
  {
  sql+=@" update T_PersonInfo set stateFlag=@stateFlag"+i+" where stateFlag=@state"+i;
  }
  sql+=@"
  if @@error<>0 
  begin  
  ROLLBACK TRANSACTION return  
  end 
  end 

  COMMIT TRANSACTION  
  ";

作者: SQL77   发布时间: 2011-12-27

引用 1 楼 ssp2009 的回复:

你给表字段需要唯一的地方加个唯一约束


主键 递增 不做添加

作者: shwicho   发布时间: 2011-12-27

引用 4 楼 sql77 的回复:

sql+=@"BEGIN TRANSACTION
begin
if not exists(select * from B_ArchiveList where relationInfo=@relation and status='true')
begin
I……


老兄理解错误,这样错误就大了去了

作者: shwicho   发布时间: 2011-12-27

字符串和int类型i是不能相加的,你给的代码能编译过去?

作者: ssp2009   发布时间: 2011-12-27

if not exists(select * from B_ArchiveList where relationInfo=@relation and status='true') 
这个判断是不可少的,请问如何来修改这个程序

作者: shwicho   发布时间: 2011-12-27

引用 7 楼 ssp2009 的回复:

字符串和int类型i是不能相加的,你给的代码能编译过去?


可以啊

作者: shwicho   发布时间: 2011-12-27

这个理解上来说只能是@@ERROR = 0了这样才能往下走。但为什么没有出错我们没有办法分析。

退一步讲,以下这个变动好像也没有意义。。。
SQL code

         begin
                Insert into B_ArchiveList ...
                if @@error<>0 
                begin
                    ROLLBACK TRANSACTION 
                    return
                end

         end
                


作者: shshjun   发布时间: 2011-12-27

这个理解上来说只能是@@ERROR = 0了这样才能往下走。但为什么没有出错我们没有办法分析。

退一步讲,以下这个变动好像也没有意义。。。
SQL code

         begin
                Insert into B_ArchiveList ...
                if @@error<>0 
                begin
                    ROLLBACK TRANSACTION 
                    return
                end

         end
                


作者: shshjun   发布时间: 2011-12-27

引用 11 楼 shshjun 的回复:

这个理解上来说只能是@@ERROR = 0了这样才能往下走。但为什么没有出错我们没有办法分析。

退一步讲,以下这个变动好像也没有意义。。。
SQL code

begin
Insert into B_ArchiveList ...
if @@error<>0
begin
……


在继续来人分析 sql 大板的人呢

作者: shwicho   发布时间: 2011-12-27