Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL SERVER-超时已过期。操作完成前的超时时间或服务器没有响应_Sql_Sql Server - Fatal编程技术网

SQL SERVER-超时已过期。操作完成前的超时时间或服务器没有响应

SQL SERVER-超时已过期。操作完成前的超时时间或服务器没有响应,sql,sql-server,Sql,Sql Server,我在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 @PresStat

我在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进行检查,结果返回了3。然后,您还有其他问题。您试图使用命名事务的事实表明可能存在其他事务?除非你是大师,我不是,否则不要使用命名嵌套事务你介意帮我编辑我的代码吗?所以我能理解我的问题在哪里。老实说,我是SQL的新手。哇,太好了,谢谢@MitchWheat。我必须将BEGIN事务置于IF之上,以便它与回滚或提交匹配。谢谢你们如果您想检查是否存在行,并根据结果执行其他一些工作,那么它应该全部位于一个事务中。否则,可能会在选中的时间和尝试插入的时间之间创建行。