读书人

帮忙看个事务 有关问题

发布时间: 2012-03-24 14:00:46 作者: rapoo

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

读书人网 >SQL Server

热点推荐