这个(简单的)sql 事务有什么问题?
“else”部分总是在sql中执行,知道为什么吗?
use myDB
GO
begin TRANSACTION
go
declare @rowtoalter int =1
update myTempTable set name='newName' where userid=1
if(@@ROWCOUNT=@rowtoalter)
begin
commit
print N'yes it works'
end
else
begin
rollback
print N'nooo'
end
GO
the "else" section is always executed in the sql, any idea why?
use myDB
GO
begin TRANSACTION
go
declare @rowtoalter int =1
update myTempTable set name='newName' where userid=1
if(@@ROWCOUNT=@rowtoalter)
begin
commit
print N'yes it works'
end
else
begin
rollback
print N'nooo'
end
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您更改的行数少于或多于一行,因为
userid=1
有零行或 2 行以上。SELECT COUNT(*) FROM myTempTable WHERE userid=1
给出了什么?我刚刚测试了在哪里更新一行完全,我得到“是的,它有效”
You are altering less than or more than one row because you have zero or 2+ rows for
userid=1
. What doesSELECT COUNT(*) FROM myTempTable WHERE userid=1
give?I've just tested where I update one row exactly and I get "yes it works"
为什么不直接将检查合并到您的更新中:
why not just incorporate the check right into your update: