![](/img/trans.png)
[英]Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding
[英]SQL SERVER - Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding
我在SQL查詢中遇到了一些問題。 以下錯誤是:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
這是我的SQL查詢
ALTER PROC sp_savepresence
@Username char(20),
@Image text
AS
BEGIN
------------
DECLARE @PresStatus CHAR,
@DateDiff INT,
@ClockIn DATETIME,
@InsertData varchar(20) = 'TranSavePresence';
IF NOT EXISTS(SELECT Username FROM PresenceTransaction WHERE Username=@Username AND ClockOut IS NULL)
BEGIN
BEGIN TRANSACTION @InsertData
INSERT INTO PresenceTransaction
(
Username,
[Image],
PresenceStatus,
WorkHour,
ClockIn,
ClockOut
)
VALUES
(
@Username,
@Image,
'N',
0,
getdate(),
NULL
)
END
ELSE
BEGIN
SELECT @ClockIn = ClockIn, @DateDiff = DateDiff(MINUTE, @ClockIn, getDate()) FROM PresenceTransaction WHERE Username=@Username AND ClockOut IS NULL AND PresenceStatus = 'N'
IF @DateDiff IS NOT NULL
BEGIN
SELECT @PresStatus = 'P'
END
ELSE
BEGIN
SELECT @PresStatus='N'
END
UPDATE PresenceTransaction
SET
PresenceStatus = @PresStatus,
WorkHour = @DateDiff,
ClockOut = getDate()
WHERE Username=@Username AND ClockOut IS NULL AND PresenceStatus = 'N'
END
------------
IF(@@Error <> 0)
BEGIN
PRINT @@Error
Rollback Tran @InsertData
SELECT @@Error AS [Status]
END
ELSE
BEGIN
COMMIT TRAN @InsertData
SELECT 'True' AS [Status]
END
END
GO
我已經從互聯網上的一些文章中讀到了一些文章,請告訴我調整我的查詢。 但我不知道錯誤點或死鎖點在哪里,我不知道如何調整我的查詢代碼。 謝謝 :)
存儲過程代碼有條件地啟動事務,但是如果沒有錯誤則提交,而不是檢查事務是否正在進行。 見@@TRANCOUNT
。
您嘗試使用命名事務的事實表明還有其他事務可能處於活動狀態。 除非你是一個大師( 我不是 ),否則我強烈建議你不要使用命名的嵌套事務。 很難做到正確並經常導致令人困惑,難以維護的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.