)[主要] @mememail varchar(255) set @memname =''John Doe'' set @mememail =''j ** *@doe.com'' 开始转发 选择@memid = min(memid) 来自tblmembers with(updlock) where activated = 0 update tblmembers set activated = 1,memname = @memname,mememail = @mememail 其中memid = @memid 提交 选择@memid 因为你要返回memid,但是更新其他列,你不能 使用我在链接中建议的UPDATE语法 - 你必须使用 锁定提示,它必须在一个事务中。请注意,我没有在上面的代码中添加任何错误处理,但你绝对应该在你的真实代码中加上 。这是一个有用的资源: http: //www.sommarskog.se/error-handling-II.html 另外一点是同时使用recid和memid列似乎是 冗余 - 表的自然主键似乎是memid(并且 mememail也可能是候选键),所以目前还不清楚recid的目的是什么? 服务,虽然我感谢您可能无法完全控制 架构,并且您可能在这里简化了您的实际数据。 SimonI think this is what you''re looking for, assuming that your definition ofthe ''next'' row is the one with the lowest value for memid within the set ofrows which have an activated value of 0:declare @memid int,@memname varchar(50),@mememail varchar(255)set @memname = ''John Doe''set @mememail = ''j***@doe.com''begin transelect @memid = min(memid)from tblmembers with(updlock)where activated = 0update tblmembersset activated = 1, memname = @memname, mememail = @mememailwhere memid = @memidcommitselect @memidSince you''re returning the memid, but updating the other columns, you can''tuse the UPDATE syntax I suggested in the link - you''ll have to use thelocking hint, which needs to be inside a transaction. Note that I haven''tput any error handling in the code above, but you should definitely put itin your real code. Here is a helpful resource: http://www.sommarskog.se/error-handling-II.htmlOne other point is that having both the recid and memid columns seems to beredundant - the natural primary key of the table appears to be memid (andmememail may also be a candidate key), so it''s not clear what purpose recidserves, although I appreciate that you may not have complete control overthe schema, and that you may have simplified your real data here.Simon 这篇关于可以在SQL Server 2000中的存储过程中锁定行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 15:57