Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Weird SQL server cursor issue

$
0
0

Hi All.

I have a stored procedure which can be seen below. The way it should work is that it should loop through a lot of all the databases on the SQL instance and pass the details onto another stored procedure. The stored procedure is called from SQL agent, and what i am finding is that when executed from SQL agent it only calls 2 databases. The first 2 on the list. There are 6 other databases, it ignores them. If I call the stored procedure from outside of SQL agent, then it covers all the databases that it should cover. Its really strange and I cannot think of why we have this behaviour.

When I run thesame duplicate setup on UAT, it works fine. Everything in terms of the stored procedure is exactly thesame. I am only seeing this issue on production. Any ideas please.

CREATE procedure [dbbackup].[spbackup_databases] (@BackupSysyemDB bit = 1,@BackupUserDB bit = 1, @BackupType char(1) = 'D', @BackupDB varchar(128) = NULL ) 
as
Begin


	DECLARE @ActiveBackupID [uniqueidentifier] 
	DECLARE @ActiveEnabled char(1)
	DECLARE @ActiveBackupDb varchar(128)
	DECLARE @ActiveBackupOrder tinyint
	DECLARE @ActiveBackupDirectory nvarchar(max)
	DECLARE @ActiveBackupType nvarchar(15)
	DECLARE @ActiveFullRetainHours int
	DECLARE @ActiveDiffRetainHours int
	DECLARE @ActiveLogRetainHours int
	DECLARE @ActiveVerifyBackup char(1)
	DECLARE @ActiveCompress char(1)
	DECLARE @ActiveCopyOnly char(1)
	DECLARE @BackupBlocksize int
	DECLARE @ActiveChangeBackupType char(1)
	DECLARE @ActiveBackupSoftwareID tinyint
	DECLARE @ActiveEnableChecksum char(1)
	DECLARE @ActiveBackupBlocksize int
	DECLARE @ActiveBackupMaxTransferSize int
	DECLARE @ActiveBackupStripes tinyint
	DECLARE @ActiveCompressionLevel tinyint
	DECLARE @ActiveBackupDescription nvarchar(max)
	DECLARE @ActiveThreads tinyint
	DECLARE @ActiveThrottle tinyint
	DECLARE @ActiveEncrypt char(1)
	DECLARE @ActiveEncryptionAlgorithm varchar(30)
	DECLARE @ActiveServerCertificate nvarchar(max)
	DECLARE @ActiveEncryptionKey nvarchar(max)
	DECLARE @ActiveBackupReadWriteFileGroups char(1)
	DECLARE @ActiveOverrideBackupPreference char(1)
	DECLARE @ActiveLogAction char(1)
	DECLARE @ActiveExecuteAction char(1)
	DECLARE @ActiveBackupBufferCount int
	DECLARE @ActiveBackupSystemDb varchar(128)
	DECLARE @ActiveSystemBackupDirectory nvarchar(max)
	DECLARE @SystemBackupType char(1)
	DECLARE @ActiveSystemVerifyBackup char(1)
	DECLARE @ActiveSystemFullRetainHours int
	DECLARE @ActiveRetentionHours int


	DECLARE @DefaultEnabled  char(1)
	DECLARE @DefaultBackupOrder  tinyint
	DECLARE @DefaultBackupDirectory  nvarchar(MAX)
	DECLARE @DefaultBackupType  char(1)
	DECLARE @DefaultFullRetainHours  int
	DECLARE @DefaultDiffRetainHours  int
	DECLARE @DefaultLogRetainHours  int
	DECLARE @DefaultVerifyBackup  char(1)
	DECLARE @DefaultCompress  char(1)
	DECLARE @DefaultCopyOnly  char(1)
	DECLARE @DefaultChangeBackupType  char(1)
	DECLARE @DefaultBackupSoftwareID  tinyint
	DECLARE @DefaultEnableChecksum  char(1)
	DECLARE @DefaultBackupBlocksize  int
	DECLARE @DefaultBackupBufferCount  int
	DECLARE @DefaultBackupMaxTransferSize  int
	DECLARE @DefaultBackupStripes  tinyint
	DECLARE @DefaultCompressionLevel  tinyint
	DECLARE @DefaultBackupDescription  nvarchar(MAX)
	DECLARE @DefaultThreads  tinyint
	DECLARE @DefaultThrottle  tinyint
	DECLARE @DefaultEncrypt  char(1)
	DECLARE @DefaultEncryptionAlgorithm  varchar(30)
	DECLARE @DefaultServerCertificate  nvarchar(MAX)
	DECLARE @DefaultEncryptionKey  nvarchar(MAX)
	DECLARE @DefaultBackupReadWriteFileGroups  char(1)
	DECLARE @DefaultOverrideBackupPreference  char(1)
	DECLARE @DefaultLogAction  char(1)
	DECLARE @DefaultExecuteAction  char(1)
	DECLARE @BackupDirectory nvarchar(255)
	DECLARE @Error int
	DECLARE @ErrorMessage nvarchar(MAX)
	DECLARE @ErrorMessageOriginal nvarchar(MAX)
	DECLARE @DefaultBackupKeysFolder nvarchar(maX)
	DECLARE @DefaultBackupCertificatesFolder nvarchar(maX)
	DECLARE @DefaultBackupSystemDbsFolder nvarchar(maX)
	DECLARE @DefaultSnapshotLocation nvarchar(maX)


------------------------------------------------
-- Initiate Backup Configuration Default Values
------------------------------------------------

	EXEC dbbackup.spinitiatebackup

-----------------------------------------------------
--	Load defaults and insert into table if not present.
--  The default is full database backups as standard
-----------------------------------------------------

	SELECT 
		   @DefaultEnabled = [Enabled]
		  ,@DefaultBackupOrder = [BackupOrder]
		  ,@DefaultBackupDirectory = [BackupDirectory]
		  ,@DefaultBackupType = [BackupType]
		  ,@DefaultFullRetainHours = [FullRetainHours]
		  ,@DefaultDiffRetainHours   = [DiffRetainHours]
		  ,@DefaultLogRetainHours   = [LogRetainHours]
		  ,@DefaultVerifyBackup   = [VerifyBackup]
		  ,@DefaultSnapshotLocation = [DBCCFolder]
		  ,@DefaultCompress   = [Compress]
		  ,@DefaultCopyOnly   = [CopyOnly]
		  ,@DefaultChangeBackupType   = [ChangeBackupType]
		  ,@DefaultBackupSoftwareID   = [BackupSoftwareID]
		  ,@DefaultEnableChecksum   = [EnableChecksum]
		  ,@DefaultBackupBlocksize   = [BackupBlocksize]
		  ,@DefaultBackupBufferCount   = [BackupBufferCount]
		  ,@DefaultBackupMaxTransferSize   = [BackupMaxTransferSize]
		  ,@DefaultBackupStripes   = [BackupStripes]
		  ,@DefaultCompressionLevel   = [CompressionLevel]
		  ,@DefaultBackupDescription   = [BackupDescription]
		  ,@DefaultThreads   = [Threads]
		  ,@DefaultThrottle   = [Throttle]
		  ,@DefaultEncrypt   = [Encrypt]
		  ,@DefaultEncryptionAlgorithm   = [EncryptionAlgorithm]
		  ,@DefaultServerCertificate   = [ServerCertificate]
		  ,@DefaultEncryptionKey   = [EncryptionKey]
		  ,@DefaultBackupReadWriteFileGroups   = [BackupReadWriteFileGroups]
		  ,@DefaultOverrideBackupPreference   = [OverrideBackupPreference]
		  ,@DefaultLogAction   = [LogAction]
		  ,@DefaultExecuteAction   = [ExecuteAction]
	  FROM [dbbackup].[backupconfig]


	   
	INSERT INTO [dbbackup].[db_backupconfig]
           (
		    [BackupID]
           ,[BackupDb]
           ,[Enabled]
           ,[BackupOrder]
           ,[BackupDirectory]
           ,[BackupType]
           ,[FullRetainHours]
           ,[DiffRetainHours]
           ,[LogRetainHours]
           ,[VerifyBackup]
		   ,[DBCCSnapFolder]
           ,[Compress]
           ,[CopyOnly]
           ,[ChangeBackupType]
           ,[BackupSoftwareID]
           ,[EnableChecksum]
           ,[BackupBlocksize]
           ,[BackupBufferCount]
           ,[BackupMaxTransferSize]
           ,[BackupStripes]
           ,[CompressionLevel]
           ,[BackupDescription]
           ,[Threads]
           ,[Throttle]
           ,[Encrypt]
           ,[EncryptionAlgorithm]
           ,[ServerCertificate]
           ,[EncryptionKey]
           ,[BackupReadWriteFileGroups]
           ,[OverrideBackupPreference]
           ,[LogAction]
           ,[ExecuteAction]
		   )

  SELECT
		  newid()	
		  ,s.name
		  ,@DefaultEnabled 
		  ,@DefaultBackupOrder 
		  ,@DefaultBackupDirectory 
		  ,@BackupType 
		  ,@DefaultFullRetainHours 
		  ,@DefaultDiffRetainHours   
		  ,@DefaultLogRetainHours   
		  ,@DefaultVerifyBackup   
		  ,@DefaultSnapshotLocation 
		  ,@DefaultCompress   
		  ,@DefaultCopyOnly   
		  ,@DefaultChangeBackupType   
		  ,@DefaultBackupSoftwareID   
		  ,@DefaultEnableChecksum   
		  ,@DefaultBackupBlocksize   
		  ,@DefaultBackupBufferCount   
		  ,@DefaultBackupMaxTransferSize   
		  ,@DefaultBackupStripes   
		  ,@DefaultCompressionLevel  
		  ,@DefaultBackupDescription  
		  ,@DefaultThreads   
		  ,@DefaultThrottle   
		  ,@DefaultEncrypt   
		  ,@DefaultEncryptionAlgorithm   
		  ,@DefaultServerCertificate   
		  ,@DefaultEncryptionKey   
		  ,@DefaultBackupReadWriteFileGroups  
		  ,@DefaultOverrideBackupPreference   
		  ,@DefaultLogAction   
		  ,@DefaultExecuteAction   
	FROM	master.dbo.sysdatabases s
	WHERE	s.name not in (select BackupDb from dbbackup.[db_backupconfig] where BackupType = @BackupType)
				AND DATABASEPROPERTY([s].name, 'IsSuspect') = 0
				AND DATABASEPROPERTY([s].name, 'IsShutdown') = 0
				AND DATABASEPROPERTY([s].name, 'IsOffline') = 0
				AND DATABASEPROPERTY([s].name, 'IsInLoad') = 0
				AND DATABASEPROPERTY([s].name, 'IsInRecovery') = 0
				AND isnull(DATABASEPROPERTY([s].name, 'IsNotRecovered'),0) = 0  -- 2012 bug
				AND DATABASEPROPERTY([s].name, 'IsReadOnly') = 0
				AND [s].name NOT IN ('master', 'msdb', 'model', 'distribution', 'tempdb') 
	
-----------------------------------------------------
-- LOAD CURSOR
-----------------------------------------------------
	DECLARE db_cursor CURSOR FOR  

		SELECT 
			   s.[name]
			  ,[Enabled]
			  ,[BackupOrder]
			  ,[BackupDirectory]
			  ,bt.[BackupName] 
			  ,[FullRetainHours]
			  ,[DiffRetainHours]
			  ,[LogRetainHours]
			  ,[VerifyBackup]
			  ,[Compress]
			  ,[CopyOnly]
			  ,[ChangeBackupType]
			  ,[BackupSoftwareID]
			  ,[EnableChecksum]
			  ,[BackupBlocksize]
			  ,[BackupBufferCount]
			  ,[BackupMaxTransferSize]
			  ,[BackupStripes]
			  ,[CompressionLevel]
			  ,[BackupDescription]
			  ,[Threads]
			  ,[Throttle]
			  ,[Encrypt]
			  ,[EncryptionAlgorithm]
			  ,[ServerCertificate]
			  ,[EncryptionKey]
			  ,[BackupReadWriteFileGroups]
			  ,[OverrideBackupPreference]
			  ,[LogAction]
			  ,[ExecuteAction]
		 FROM [dbbackup].[db_backupconfig] bc
		 INNER JOIN [dbbackup].backuptype bt		 ON  bt.BackupType = bc.BackupType
		 LEFT JOIN sys.databases [s]
		 ON [s].[name] collate database_default = [bc].[BackupDb] 
		 WHERE [Enabled] = 'Y'
		 AND bc.[BackupType] = @BackupType
		 AND (@BackupDB is NULL OR bc.[BackupDb] = @BackupDB)
		 AND		@BackupUserDB = 1 

		 ORDER BY [BackupOrder] ASC

	OPEN db_cursor   
	FETCH NEXT FROM db_cursor 
	INTO 
				
				@ActiveBackupDb,
				@ActiveEnabled,			
				@ActiveBackupOrder,
				@ActiveBackupDirectory,
				@ActiveBackupType,
				@ActiveFullRetainHours,
				@ActiveDiffRetainHours,
				@ActiveLogRetainHours,
				@ActiveVerifyBackup,
				@ActiveCompress,
				@ActiveCopyOnly,
				@ActiveChangeBackupType,
				@ActiveBackupSoftwareID,
				@ActiveEnableChecksum,
				@ActiveBackupBlocksize,
				@ActiveBackupBufferCount,
				@ActiveBackupMaxTransferSize,
				@ActiveBackupStripes,
				@ActiveCompressionLevel,
				@ActiveBackupDescription,
				@ActiveThreads,
				@ActiveThrottle,
				@ActiveEncrypt,
				@ActiveEncryptionAlgorithm,
				@ActiveServerCertificate,
				@ActiveEncryptionKey,
				@ActiveBackupReadWriteFileGroups,
				@ActiveOverrideBackupPreference,
				@ActiveLogAction,
				@ActiveExecuteAction  

		WHILE @@FETCH_STATUS = 0   
		BEGIN   

		SELECT @ActiveRetentionHours = CASE
				WHEN @ActiveBackupType = 'DIFF' THEN   @ActiveDiffRetainHours
				WHEN @ActiveBackupType = 'LOG'	THEN   @ActiveLogRetainHours
				WHEN @ActiveBackupType = 'FULL' THEN   @ActiveFullRetainHours
				ELSE
					  144
				END

		IF (@ActiveBackupSoftwareID = 1) SET @ActiveBackupSoftwareID = NULL
		SET @ActiveBackupDescription = 'Backup for ' + @ActiveBackupDb + ' Type = ' + @BackupType + ' ON ' + CONVERT(VARCHAR(30),GETDATE(),113)

		select 	@ActiveBackupDb,
				@ActiveEnabled,			
				@ActiveBackupOrder,
				@ActiveBackupDirectory,
				@ActiveBackupType,
				@ActiveFullRetainHours,
				@ActiveDiffRetainHours,
				@ActiveLogRetainHours,
				@ActiveVerifyBackup,
				@ActiveCompress,
				@ActiveCopyOnly,
				@ActiveChangeBackupType,
				@ActiveBackupSoftwareID,
				@ActiveEnableChecksum,
				@ActiveBackupBlocksize,
				@ActiveBackupBufferCount,
				@ActiveBackupMaxTransferSize,
				@ActiveBackupStripes,
				@ActiveCompressionLevel,
				@ActiveBackupDescription,
				@ActiveThreads,
				@ActiveThrottle,
				@ActiveEncrypt,
				@ActiveEncryptionAlgorithm,
				@ActiveServerCertificate,
				@ActiveEncryptionKey,
				@ActiveBackupReadWriteFileGroups,
				@ActiveOverrideBackupPreference,
				@ActiveLogAction,
				@ActiveExecuteAction  


		EXEC [dbbackup].[spbackup_db]
								@Databases  = @ActiveBackupDb,
								@Directory = @ActiveBackupDirectory,
								@BackupType = @ActiveBackupType,
								@Verify  = @ActiveVerifyBackup,
								@CleanupTime  = @ActiveRetentionHours,
								@Compress = @ActiveCompress,
								@CopyOnly = @ActiveCopyOnly,
								@ChangeBackupType = @ActiveChangeBackupType,
								@BackupSoftware  = @ActiveBackupSoftwareID,
								@CheckSum  = @ActiveEnableChecksum,
								@BlockSize  = @ActiveBackupBlocksize,
								@BufferCount  = @ActiveBackupBufferCount,
								@MaxTransferSize =@ActiveBackupMaxTransferSize,
								@NumberOfFiles = @ActiveBackupStripes,
								@CompressionLevel = @ActiveCompressionLevel,
								@Description = @ActiveBackupDescription,
								@Threads  = @ActiveThreads,
								@Throttle = @ActiveThrottle,
								@Encrypt = @ActiveEncrypt,
								@EncryptionAlgorithm = @ActiveEncryptionAlgorithm,
								@ServerCertificate = @ActiveServerCertificate,
								@ServerAsymmetricKey = @ActiveEncryptionKey,
								@EncryptionKey = @ActiveEncryptionKey,
								@ReadWriteFileGroups = @ActiveBackupReadWriteFileGroups,
								@OverrideBackupPreference = @ActiveOverrideBackupPreference,
								@LogToTable  = @ActiveLogAction,
								@Execute = @ActiveExecuteAction



       FETCH NEXT FROM db_cursor 
	   INTO

				@ActiveBackupDb,
				@ActiveEnabled,			
				@ActiveBackupOrder,
				@ActiveBackupDirectory,
				@ActiveBackupType,
				@ActiveFullRetainHours,
				@ActiveDiffRetainHours,
				@ActiveLogRetainHours,
				@ActiveVerifyBackup,
				@ActiveCompress,
				@ActiveCopyOnly,
				@ActiveChangeBackupType,
				@ActiveBackupSoftwareID,
				@ActiveEnableChecksum,
				@ActiveBackupBlocksize,
				@ActiveBackupBufferCount,
				@ActiveBackupMaxTransferSize,
				@ActiveBackupStripes,
				@ActiveCompressionLevel,
				@ActiveBackupDescription,
				@ActiveThreads,
				@ActiveThrottle,
				@ActiveEncrypt,
				@ActiveEncryptionAlgorithm,
				@ActiveServerCertificate,
				@ActiveEncryptionKey,
				@ActiveBackupReadWriteFileGroups,
				@ActiveOverrideBackupPreference,
				@ActiveLogAction,
				@ActiveExecuteAction  

		END   

	CLOSE db_cursor   
	DEALLOCATE db_cursor 




------------------------------------
-- Deal with system databases
-- Read parameters
------------------------------------

	IF (@BackupSysyemDB = 1)
	BEGIN


		SELECT	@ActiveBackupSystemDb  = sysbk.[BackupDb],
				@ActiveSystemBackupDirectory = sysbk.[BackupDirectory],
				@ActiveSystemVerifyBackup = sysbk.VerifyBackup,
				@ActiveSystemFullRetainHours = sysbk.[FullRetainHours]
		FROM	[dbbackup].[system_backupconfig] sysbk
		WHERE	[BackupDb] = 'SYSTEM_DATABASES'


		EXEC [dbbackup].[spbackup_db]
									@Databases  = 'SYSTEM_DATABASES',
									@Directory = @ActiveSystemBackupDirectory,
									@Verify  = @ActiveSystemVerifyBackup,
									@CleanupTime  = @ActiveSystemFullRetainHours,
									@BackupType = 'FULL'


	END


End













GO





Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>