日期:2014-05-18  浏览次数:20426 次

帮忙看个事务 问题
插入重复数据不回滚继续执行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            
                ";



------解决方案--------------------
你给表字段需要唯一的地方加个唯一约束
------解决方案--------------------
已经判断了,就不会插入错误了,怎么会回滚呢?
------解决方案--------------------
有判断,是不是看错了,根本不会重复
------解决方案--------------------
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
";


------解决方案--------------------
字符串和int类型i是不能相加的,你给的代码能编译过去?
------解决方案--------------------
这个理解上来说只能是@@ERROR = 0了这样才能往下走。但为什么没有出错我们没有办法分析。

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

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

         end

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

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

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

         end