帮忙看个事务 问题

Wicho开源开发中 2011-12-27 04:38:30
插入重复数据不回滚继续执行Update,是什么造成的


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
";
...全文
142 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 shshjun 的回复:]

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

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

begin
Insert into B_ArchiveList ...
if @@error<>0
begin
……
[/Quote]

在继续来人分析 sql 大板的人呢
shshjun 2011-12-27
  • 打赏
  • 举报
回复
这个理解上来说只能是@@ERROR = 0了这样才能往下走。但为什么没有出错我们没有办法分析。

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

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

end


shshjun 2011-12-27
  • 打赏
  • 举报
回复
这个理解上来说只能是@@ERROR = 0了这样才能往下走。但为什么没有出错我们没有办法分析。

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

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

end


  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ssp2009 的回复:]

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

可以啊
  • 打赏
  • 举报
回复
if not exists(select * from B_ArchiveList where relationInfo=@relation and status='true')
这个判断是不可少的,请问如何来修改这个程序
快溜 2011-12-27
  • 打赏
  • 举报
回复
字符串和int类型i是不能相加的,你给的代码能编译过去?
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sql77 的回复:]

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

老兄理解错误,这样错误就大了去了
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ssp2009 的回复:]

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

主键 递增 不做添加
SQL77 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
";

昵称被占用了 2011-12-27
  • 打赏
  • 举报
回复
有判断,是不是看错了,根本不会重复
-晴天 2011-12-27
  • 打赏
  • 举报
回复
已经判断了,就不会插入错误了,怎么会回滚呢?
快溜 2011-12-27
  • 打赏
  • 举报
回复
你给表字段需要唯一的地方加个唯一约束

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧