[英]SQL SERVER - Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding
i got a little problem in my SQL Query. 我在SQL查询中遇到了一些问题。 The following error is : 以下错误是:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
And here is my SQL Query 这是我的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
I have already read from some articles over the Internet, and some of articles, tell me to tune up my query. 我已经从互联网上的一些文章中读到了一些文章,请告诉我调整我的查询。 But i don't know where's the error point or maybe deadlock point, and I don't know how to tune up my query code. 但我不知道错误点或死锁点在哪里,我不知道如何调整我的查询代码。 Thanks :) 谢谢 :)
Your stored procedure code conditionally starts a transaction, but commits if there was no error, rather than also checking if a transaction is in progress. 存储过程代码有条件地启动事务,但是如果没有错误则提交,而不是检查事务是否正在进行。 See @@TRANCOUNT
. 见@@TRANCOUNT
。
The fact you are trying to use a named transaction suggests there are other transactions likely to be active. 您尝试使用命名事务的事实表明还有其他事务可能处于活动状态。 Unless you are a guru ( and I'm not ) I would strongly suggest not using named nested transactions. 除非你是一个大师( 我不是 ),否则我强烈建议你不要使用命名的嵌套事务。 It is hard to get right and often leads to confusing, hard to maintain code. 很难做到正确并经常导致令人困惑,难以维护的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.