日期:2014-05-17 浏览次数:20598 次
BEGIN
DECLARE @tmpId INT;
--在这里开始上锁,独占这一行,不允许其他用户查询这一行
SELECT TOP 1 @tmpId=Id
FROM Task
WHERE DATEDIFF(mi,t.Last_Work_Time,GETDATE())> Interval_Time
ORDER BY Id ASC ;
UPDATE Task SET Last_Work_Time=GETDATE() WHERE Id=@tmpId;
--更新之后,解锁
SELECT Id,Keyword,Interval_Time,Last_Work_Time FROM Task WHERE Id=@tmpId;
END
BEGIN
DECLARE @tmpId INT;
--在这里开始上锁,独占这一行,不允许其他用户查询这一行
SELECT TOP 1 @tmpId=Id
FROM Task with(xlock) --加了独占锁
WHERE DATEDIFF(mi,t.Last_Work_Time,GETDATE())> Interval_Time
ORDER BY Id ASC ;
UPDATE Task SET Last_Work_Time=GETDATE() WHERE Id=@tmpId;
--更新之后,解锁
SELECT Id,Keyword,Interval_Time,Last_Work_Time FROM Task WHERE Id=@tmpId;
END
BEGIN
DECLARE @tmp table (tmpId INT)
BEGIN tran
UPDATE Task SET Last_Work_Time=GETDATE()
OUTPUT Inserted.ID INTO @tmp
WHERE Id=
(
SELECT TOP 1 Id
FROM Task
WHERE DATEDIFF(mi,t.Last_Work_Time,GETDATE())> Interval_Time
ORDER BY Id ASC
)
COMMIT TRAN
SELECT Id,Keyword,Interval_Time,Last_Work_Time FROM Task
WHERE Id=(SELECT tmpId from @tmp)
END
BEGIN
DECLARE @tmp table (Id INT,Keyword VARCHAR(MAX),Interval_Time datetime,Last_Work_Time DATETIME)
BEGIN tran
UPDATE Task SET Last_Wor