Question : MSG 3930 error in sqlserver2008

Hi,

I got the following error

Msg 3930, Sev 16, State 1, Line 29 : The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction. [SQLSTATE 42000].

the code is


CREATE PROCEDURE [dbo].[DBA_MaintenancePlan]  
   @dbList varchar(1000) = null  
   ,@AllDBs bit = 0  
   ,@UserDBs bit = 0  
   ,@SystemDBs bit = 0  
   ,@excludeList varchar(1000) = null  
   ,@BackupDB bit=0  
   ,@BackupLog bit=0  
   ,@INIT bit=0  
   ,@DIFF bit=0  
   ,@CHECKDB bit=0  
   ,@CHECKCATALOG bit=0  
   ,@CHECKALLOC bit=0  
   ,@CHECK_COMPREHENSIVE bit=0  
   ,@CHECK_DOW int = null  
   ,@OPT_REFRESH bit=0  
   ,@OPT_INDEXES bit=0  
   ,@OPT_IndexFragThreshold float=5.0  
   ,@OPT_IndexFillFactor int = 100  
   ,@OPT_IndexOnlineOperationsOnly bit = 1  
   ,@OPT_IndexBulkLogged bit = 0  
   ,@OPT_IndexAutoLogBackup bit = 0  
   ,@OPT_IndexAutoLogBackupIntervalSecs int = 300  
   ,@OPT_IndexMaxDurationMins int = 0  
   ,@OPT_IndexTableRowLimit int = 0  
   ,@OPT_STATISTICS bit=0  
   ,@OPT_StatsRowModPct decimal(19,2) = 5.0  
   ,@OPT_StatsIncludeCIXStats bit = 0  
   ,@OPT_StatsIncludeCLStats bit = 0  
   ,@OPT_StatsSamplePct int = 50  
   ,@OPT_StatsDOW int = null  
   ,@OPT_StatsMaxDurationMins int = 0  
   ,@OPT_StatsTableRowLimit int = 0  
   ,@OPT_IndexTableRowMin int = 0  
   ,@OPT_StatsTableRowMin int = 0  
AS BEGIN  
   SET NOCOUNT ON  
 
   CREATE TABLE #DB(id int identity, DBID int NOT NULL, name SYSNAME NOT NULL)  
 
   SELECT DBID, name  
   into #allDBs  
   FROM master.dbo.sysdatabases  
   WHERE 1=1  
      and name <> 'tempdb'  
      and @AllDBs = 1  
 
   SELECT DBID, name  
   into #systemDBs  
   FROM master.dbo.sysdatabases  
   WHERE 1=1  
      and name IN ('master', 'model', 'msdb', 'distribution')  
      and @SystemDBs = 1  
 
   SELECT DBID, name  
   into #userDBs  
   FROM master.dbo.sysdatabases  
   WHERE 1=1  
      and name NOT IN ('master', 'model', 'msdb', 'tempdb', 'distribution')  
      and @UserDBs = 1  
 
 
   INSERT INTO #DB(dbid, name)  
      select DBId, name  
      from #allDBs  
      WHERE name not in (select rtrim(ltrim(Element)) from dbo.fn_Split(@excludeList, ','))  
      UNION  
      select DBID, name  
      from #systemDBs  
      WHERE name not in (select rtrim(ltrim(Element)) from dbo.fn_Split(@excludeList, ','))  
      UNION  
      select DBID, name  
      from #userDBs  
      WHERE name not in (select rtrim(ltrim(Element)) from dbo.fn_Split(@excludeList, ','))  
      UNION  
      SELECT DBID,name  
      FROM master.dbo.sysdatabases  
      WHERE name IN (select rtrim(ltrim(Element)) from dbo.fn_Split(@dbList, ','))  
         and name not in (select rtrim(ltrim(Element)) from dbo.fn_Split(@excludeList, ','))  
      ORDER BY name  
 
   DECLARE @DBID int, @id int, @lastId int,  
      @Database sysname,  
      @rtn int,  
      @sqlstring nvarchar(250),  
      @setting char(10),  
      @Message nvarchar(1024),  
      @start_dow int,  
      @skip_check bit,  
      @skip_stats bit,  
      @compName varchar(100)  
 
   select @start_dow = DatePart(dw, GetDate()), @skip_check = 0, @skip_stats = 0  
   if (@check_dow is not null and @check_dow <> @start_dow) BEGIN  
      set @skip_check = 1  
      print 'Skipping database consistency checks.  StartDOW:' + cast(@start_dow as varchar) + ', CheckDOW:' + cast(@check_dow as varchar)  
   end  
 
   if (@OPT_StatsDOW is not null and @OPT_StatsDOW <> @start_dow) BEGIN  
      set @skip_stats = 1  
      print 'Skipping statistic optimizations.  StartDOW:' + cast(@start_dow as varchar) + ', StatsDOW:' + cast(@OPT_StatsDOW as varchar)  
   end  
 
 
     
   select @id = 1, @lastId = max(id) from #DB  
 
   WHILE (@id <= @lastId) BEGIN  
 
      SELECT @DBID = DBID, @Database = UPPER(name)  
      FROM  #DB  
      WHERE ID = @id  
 
      set @id = @id + 1  
 
      set @compName = QUOTENAME(@@SERVERNAME) + '.' + @database  
 
      -- run any checks  
      BEGIN TRY  
         if ((@checkdb = 1 or @checkalloc = 1 or @checkcatalog = 1) and (@skip_check = 0)) BEGIN  
            EXEC @rtn = dba..DBA_CheckDatabase @database=@database  
               ,@checkdb=@checkdb  
               ,@checkalloc=@checkalloc  
               ,@checkcatalog=@checkcatalog  
               ,@check_comprehensive=@check_comprehensive  
 
            IF (@rtn <> 0) BEGIN  
               CONTINUE  
            END  
         END  
 
         if (@OPT_REFRESH = 1) BEGIN  
            EXEC @rtn = dba..DBA_RefreshOptimizationStatus @database=@database  
         END  
 
         if (@OPT_INDEXES = 1) BEGIN  
            EXEC @rtn = dba..DBA_PerformIndexOptimizations @database=@database  
               , @fragThreshold=@OPT_IndexFragThreshold  
               , @fillFactor=@OPT_IndexFillFactor  
               , @onlineOperationsOnly=@OPT_IndexOnlineOperationsOnly  
               , @bulkLogged=@OPT_IndexBulkLogged  
               , @autoLogBackup=@OPT_IndexAutoLogBackup  
               , @autoLogBackupIntervalSecs=@OPT_IndexAutoLogBackupIntervalSecs  
               , @maxDurationMins = @OPT_IndexMaxDurationMins  
               , @tableRowLimit = @OPT_IndexTableRowLimit  
               , @tableRowMin = @OPT_IndexTableRowMin  
         END  
 
         if (@OPT_STATISTICS = 1 and @skip_stats = 0) BEGIN  
            EXEC @rtn = DBA_PerformStatisticOptimizations @database=@database  
               , @rowModPct=@OPT_StatsRowModPct  
               , @includeCIXStats=@OPT_StatsIncludeCIXStats  
               , @includeCLStats=@OPT_StatsIncludeCLStats  
               , @samplePct=@OPT_StatsSamplePct  
               , @maxDurationMins = @OPT_StatsMaxDurationMins  
               , @tableRowLimit = @OPT_StatsTableRowLimit  
               , @tableRowMin = @OPT_StatsTableRowMin  
         END  
 
         if (@BackupDB = 1) BEGIN  
            EXEC @rtn = dba..DBA_BackupDatabase @database=@database, @INIT=@INIT, @DIFF=@DIFF, @dow=@start_dow  
 
            IF (@rtn <> 0) BEGIN  
               CONTINUE  
            END  
             
            -- after a full or diff backup, the transaction log backup can be re-initialized to save space  
            if (@INIT = 1) BEGIN  
               exec @rtn = dba..DBA_BackupLog @database=@database, @INIT=1, @NOWARN=1, @dow=@start_dow  
 
               IF (@rtn <> 0) BEGIN  
                  CONTINUE  
               END  
            END  
         END  
         ELSE if (@BackupLog = 1) BEGIN  
            exec @rtn = dba..DBA_BackupLog @database=@database, @INIT=@INIT, @dow=@start_dow  
 
            IF (@rtn <> 0) BEGIN  
               CONTINUE  
            END  
         END  
      END TRY  
      BEGIN CATCH  
         set @message = error_message()  
         exec master..usp_LogDBAError @compName, @message  
         IF (XACT_STATE()) = -1  
         BEGIN  
           ROLLBACK  
         END  
          CONTINUE  
      END CATCH  
   END -- while  
 
   RETURN 0  
END  

could you please modify above code send it to me

thanks in advance

Answer : MSG 3930 error in sqlserver2008

Then try:

         IF (XACT_STATE()) = -1  
         BEGIN  
           ROLLBACK  
         END  
          COMMIT
CONTINUE
      END CATCH  
   END -- while  
     RETURN 0  
END  
Random Solutions  
 
programming4us programming4us