下面的SQL语句是否安全'用于并发访问? A
被锁定在什么时候?如果它在INSERT
之前,那么第一个@count
可能出错?
BEGIN TRAN;
SELECT @count = count(1) from A
DELETE FROM A where x=z
SELECT @newCount = count(1) from A
SELECT @newCount - @count
COMMIT TRAN;
答案 0 :(得分:1)
BEGIN TRAN;
DECLARE @RC INT
EXEC @RC = sp_getapplock @Resource='ArchiveLock', @LockMode='Exclusive', @LockOwner='Transaction', @LockTimeout=15000
SELECT @count = count(1) from A
DELETE FROM A where x=z
SELECT @newCount = count(1) from A
SELECT @newCount - @count
COMMIT TRAN;
试试这个。我最近使用它来处理由多个进程并行调用的Delet语句集。 sp_getapplock会创建一个锁,直到提交事务,这会使其他进程等待。
希望这有帮助!