【发布时间】:2010-10-01 16:49:26
【问题描述】:
很简单的问题。在 SQL 2008 中,如果我有一个存储过程(见下文),我是否会冒前两个语句之间出现竞争条件的风险,或者存储过程是否会像事务一样锁定它所触及的事物?
ALTER PROCEDURE [dbo].[usp_SetAssignedTo]
-- Add the parameters for the stored procedure here
@Server varchar(50),
@User varchar(50),
@UserPool varchar(50)
AS
BEGIN
SET NOCOUNT ON;
Declare @ServerUser varchar(50)
-- Find a Free record
SELECT top 1 @ServerUser = UserName
from ServerLoginUsers
where AssignedTo is null and [TsServer] = @Server
--Set the free record to the user
Update ServerLoginUsers
set AssignedTo = @User, AssignedToDate = getdate(), SourcePool = @UserPool
where [TsServer] = @Server and UserName = @ServerUser
--report record back if it was updated. Null if it was not available.
select *
from ServerLoginUsers
where [TsServer] = @Server
and UserName = @ServerUser
and AssignedTo = @User
END
【问题讨论】:
标签: sql-server tsql sql-server-2008 stored-procedures concurrency