【问题标题】:The current transaction cannot be committed and cannot support operations that write to the log file. Rollback the transaction当前事务无法提交,也无法支持写入日志文件的操作。回滚事务
【发布时间】:2019-10-30 04:28:58
【问题描述】:

我在 SQL Server 2008 中运行我的脚本时遇到了这个错误。但是当我在 SQL Server 2012 中恢复我的数据库时,它成功运行并且我没有遇到任何错误。

当前事务无法提交,也无法支持写入日志文件的操作。回滚事务。

这是我的存储过程:

CREATE PROCEDURE UpdateDependentViews  
(                                                              
 @TableName NVARCHAR(128),  
 @AllDependents bit = 1  
)                           
AS                                                              
    SET NOCOUNT ON;

    CREATE TABLE #Dependencies
    (
       [Counter] [int] IDENTITY(1,1) NOT NULL,
       [View_Name] [nvarchar](128),                          
    ) ON [PRIMARY];

    CREATE INDEX Counter ON #Dependencies(Counter);

    /* Get the first degree dependent views. */  
    INSERT INTO #Dependencies(View_Name)  
       SELECT V.[name] AS [View_Name]  
       FROM sys.sql_expression_dependencies SD  
       INNER JOIN sys.views V ON SD.referencing_id = V.object_id  
       INNER JOIN sys.objects D ON SD.referenced_id = D.object_id  
       WHERE SD.referencing_minor_id = 0  
         AND SD.referenced_minor_id = 0  
         AND SD.referencing_class = 1   
         AND D.type IN ('U', 'V')  
         AND D.is_ms_shipped = 0  
         AND V.is_ms_shipped = 0  
         AND D.[name] = @TableName  

    SELECT * 
    FROM sys.sql_expression_dependencies  
    WHERE referenced_entity_name IS NULL  

    /* Refresh the dependent views. */                          
    DECLARE @ViewName NVARCHAR(128)                          
    DECLARE @Counter INT  
SET @Counter = 1;  
DECLARE @Errors NVARCHAR(MAX)  
SET @Errors = ''  
WHILE EXISTS (SELECT [View_Name] FROM #Dependencies WHERE Counter = @Counter)                      
BEGIN                      
 SELECT @ViewName = View_Name FROM #Dependencies WHERE Counter = @Counter;                      
 /*Get Inner view dependencies */                      
 IF ISNULL(@AllDependents, 0) = 1   
 BEGIN     
  IF ISNULL(@AllDependents, 0) = 1 AND EXISTS(SELECT 1   
             FROM sys.sql_expression_dependencies SD   
             INNER JOIN sys.objects D ON SD.referenced_id =  D.object_id  
             WHERE SD.referencing_minor_id = 0  
              AND SD.referenced_minor_id = 0  
              AND SD.referencing_class = 1  
              AND D.type IN ('U', 'V')  
              AND D.is_ms_shipped = 0  
              AND D.[name] = @ViewName)  
  BEGIN  
   INSERT INTO #Dependencies(View_Name)  
   SELECT V.[name] AS [View_Name]  
   FROM sys.sql_expression_dependencies SD  
   INNER JOIN sys.views V  
    ON SD.referencing_id =  V.object_id  
   INNER JOIN sys.objects D  
    ON SD.referenced_id =  D.object_id  
   WHERE SD.referencing_minor_id = 0  
    AND SD.referenced_minor_id = 0  
    AND SD.referencing_class = 1   
    AND D.type IN ('U', 'V')  
    AND D.is_ms_shipped = 0  
    AND V.is_ms_shipped = 0  
    AND ISNULL(D.[name], '') <> ''    
    AND D.[name] = @ViewName  
    AND V.[name] NOT IN (SELECT View_Name FROM #Dependencies )        
  END  
 END;  

 /* Refresh the view */   
 BEGIN TRY  
     --BEGIN TRANSACTION  
  EXEC SP_REFRESHVIEW @ViewName  
  --COMMIT TRANSACTION    
 END TRY  
 BEGIN CATCH  
     --ROLLBACK TRANSACTION  
  IF EXISTS (SELECT 1 FROM [ISSIMODEL(15)].sys.objects WHERE [name] = @ViewName)  
   SET @Errors = @Errors + CHAR(13) + 'Error: Failed to RefreshView ' + @ViewName + '. Message: ' + ERROR_MESSAGE()   
 END CATCH  

 SET @Counter = @Counter + 1;  
END;  
IF ISNULL(@Errors, '') <> ''  
 RAISERROR (@Errors, 16, 1)  

DROP TABLE #Dependencies;  

【问题讨论】:

  • 您没有提供足够的信息来得出任何结论。
  • 能否请您发布导致此问题的脚本?
  • “我在运行我的脚本时遇到了这个错误” - 帮助我们帮助你,请发布你的脚本。

标签: sql-server


【解决方案1】:

当您在代码中使用 TRY\CATCH 块时,通常会发生这种情况: 确保您的代码中有此流程:

DECLARE @ErrorMum INT 
DECLARE @ErrorMessage VARCHAR(200) 
DECLARE @Severity INT 
DECLARE @State INT 

BEGIN TRY

    BEGIN TRAN T1

    <Your Code goes here>

    COMMIT TRAN T1 
END TRY

BEGIN CATCH
    IF @@TRANCOUNT > 0 BEGIN 
    ROLLBACK TRAN T1 
    END

    SET @ErrorMum = ERROR_NUMBER()
    SET @ErrorMessage = ERROR_MESSAGE()
    SET @Severity = ERROR_SEVERITY()
    SET @State = ERROR_STATE()

    RAISERROR(@ErrorMessage, @Severity, @State)
END CATCH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多