SQL Server存储过程随机失败

时间:2013-12-17 04:32:04

标签: exception stored-procedures sql-server-2008-r2

我有以下存储过程:

CREATE PROCEDURE [dbo].[sp_cache_remove]  
 @Key NVARCHAR(450)  
AS  
BEGIN  

 --Clean up first  
 DELETE FROM Cache WHERE Expire <= GETUTCDATE()  

 DECLARE @Result NVARCHAR(MAX)  
 SELECT TOP 1 @Result = Value   
 FROM Cache  
 WHERE [Key] = @Key AND  
   Expire > GETUTCDATE()  

 DELETE FROM Cache WHERE [Key] = @Key  

 SELECT @Result  

END  

这个随机失败。当我注释掉第一行(DELETE FROM...)时,它工作正常。这只发生在生产服务器上,因此无法获取更多错误详细信息。任何人都可以在这里发现任何可能的问题吗?

编辑:还有其他存储过程可以添加,获取等等。是否会出现死锁或什么?

1 个答案:

答案 0 :(得分:1)

你根本没有找回任何尝试initaliazed @Result

CREATE PROCEDURE [dbo].[sp_cache_remove]  
 @Key NVARCHAR(450)  
AS  
BEGIN  

 --Clean up first  
 DELETE FROM Cache WHERE Expire <= GETUTCDATE()  

 DECLARE @Result NVARCHAR(MAX) 
    set @Result = ''        -- <---- Initialized variable
 SELECT TOP 1 @Result = Value   
 FROM Cache  
 WHERE [Key] = @Key AND  
   Expire > GETUTCDATE()  

 DELETE FROM Cache WHERE [Key] = @Key  

 SELECT @Result  

END