Why is my Ola Hallengren log back up retention not clearing down old log files? - transaction-log

I have set up Ola Hallengren in a UAT environment to perform transaction log back ups with a 2 day retention on AOAG databases on a secondary node. On the same node I am also taking copy_only database back ups. You will see from my code that I am specifying 'User_Databases' as opposed to specifying the AOAG. The reason for this is because I need to exclude some AOAG DBs from the db back up job and therefore used the same approach on the tlog back up job.
trn:
EXECUTE [dbo].[DatabaseBackup]
#Databases = 'USER_DATABASES',
#Directory = N'G:\SQLLogs\UserDB',
#BackupType = 'LOG',
#Verify = 'Y',
#CopyOnly = 'N',
#CleanupTime = 48,
#CheckSum = 'Y',
#overridebackuppreference = 'Y',
#availabilitygroupdirectorystructure = '{DatabaseName}_{BackupType}_{CopyOnly}',
#AvailabilityGroupFileName = '{DatabaseName}_{Year}{Month}{Day}_{Hour}{Minute}{Second}_{FileNumber}.{FileExtension}',
#directorystructure = '{DatabaseName}_{BackupType}_{CopyOnly}',
#FileName = '{DatabaseName}_{Year}{Month}{Day}_{Hour}{Minute}{Second}_{FileNumber}.{FileExtension}'
bak:
EXECUTE [dbo].[DatabaseBackup]
#Databases = 'USER_DATABASES, -%excludeDB%',
#Directory = N'E:\SQLBackups\UserDB',
#BackupType = 'FULL',
#Verify = 'Y',
#CopyOnly = 'Y',
#CleanupTime = 48,
#CheckSum = 'Y',
#overridebackuppreference = 'Y',
#availabilitygroupdirectorystructure = '{DatabaseName}_{BackupType}_{CopyOnly}',
#AvailabilityGroupFileName = '{DatabaseName}_{Year}{Month}{Day}_{Hour}{Minute}{Second}_{FileNumber}.{FileExtension}',
#directorystructure = '{DatabaseName}_{BackupType}_{CopyOnly}',
#FileName = '{DatabaseName}_{Year}{Month}{Day}_{Hour}{Minute}{Second}_{FileNumber}.{FileExtension}'
The bak and trn back ups complete as expected and the baks are cleared down when older than 48 hours, however the log back ups build up.
Can anyone see what is wrong here that is preventing the logs from clearing down?

Related

CSV file from the SQL Server is out of format

I am new to SQL Server and trying to send a CSV file as an email attachment to business users. Unfortunately, the file received is totally out of format. I have the following code and the table is from SAP B1. What would be the best way to resolve this?
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'profile name',
#recipients = 'recipent name',
#subject = 'Test',
#query = 'select slpcode, slpname, memo, commission from dbo.OSLP ',
#attach_query_result_as_file = 1,
#query_attachment_filename = 'Results.csv',
#query_result_separator = ',',
#query_result_width = 32767,
#query_result_header = 1,
#append_query_error = 1,
#query_no_truncate = 0
edit: Ideally, the data should be shown in only 4 columns as I have queried only 4 columns but instead this information is spread across columns A:N
Any help would be greatly appreciated.
The sample data in the table looks as follows:
Sample Data Link
and the result from my code shows up as follows:
Final Result Link
Thanks,

How Do I Save SQL Server Backup with Date Filename

Hi can I convert the following code so that the backup includes the date and time in the filename.
USE SysproCompany
GO
EXEC('BACKUP DATABASE [Company] TO DISK = N''G:\SQLBackups\INSTANCE1\MonthEndBackup\Company\MonthEndBackup.bak'' WITH NOFORMAT, COPY_ONLY,INIT,
NAME = N''MonthEndBackup'', SKIP, NOREWIND, NOUNLOAD, STATS = 10
')
PRINT 'Backup of Syspro Company started'
GO
PRINT 'Backup of Syspro Company complete'
You don't need exec to begin with. Just execute
BACKUP DATABASE [Company]
TO DISK = N''G:\SQLBackups\INSTANCE1\MonthEndBackup\Company\MonthEndBackup.bak''
WITH NOFORMAT, COPY_ONLY,INIT,
NAME = N''MonthEndBackup'', SKIP, NOREWIND, NOUNLOAD, STATS = 10
You'll have to specify the file name yourself, paths don't use formatting characters or patterns. After all, different DBAs use completely different folder and naming conventions. You can calculate the name in a variable before you use it.
A far better option though, the one used by most DBAs, is to use Ola Hallengren's backup scripts to specify settings, folder and filename patterns, for multiple databases at a time.
The default pattern filename pattern is
'{ServerName}${InstanceName}_{DatabaseName}_{BackupType}_{Partial}_{CopyOnly}_{Year}{Month}{Day}_{Hour}{Minute}{Second}_{FileNumber}.{FileExtension}',
A single command can be used to perform multiple operations, eg both backup all user databases and verify the backups, and clean up old backups :
EXECUTE dbo.DatabaseBackup
#Databases = 'USER_DATABASES',
#Directory = 'C:\Backup',
#BackupType = 'FULL',
#Verify = 'Y',
#Compress = 'Y',
#CheckSum = 'Y',
#CleanupTime = 24
Try this:
EXEC('BACKUP DATABASE [Company] TO DISK = N''G:\SQLBackups\INSTANCE1\MonthEndBackup\Company\MonthEndBackup' + CURRENT_TIMESTAMP + '.bak'' WITH NOFORMAT, COPY_ONLY,INIT,
NAME = N''MonthEndBackup'', SKIP, NOREWIND, NOUNLOAD, STATS = 10
')

SQL Server: DB Mail Not getting triggered after certain Size

I am sending csv file from SQL Server using below query.
EXEC #RtnCode = sp_send_dbmail
#profile_name = 'Mail',
#query_result_separator=#Separateur,
#recipients = 'xxx#xxx.com;',
#subject = #mailSubject,
#query = #STMT,
#Attach_Query_result_as_file = 1,
#query_attachment_filename = 'filename.csv',
#query_result_header = 1,
#query_result_width = 32767,
#query_result_no_padding = 1
Mail size in 'Database Mail' was configured as 1 MB. I have changed it to 5 MB. And restarted SQL Server agent. But Mails are not getting triggered, even though it is getting queued in Mail Queue.
Please help,
Thanks in advance.

change data capture - monitor single column

Afternoon all,
I've enabled CDC on a particular table (code below) however I'm wondering if it's possible to track changes on LocationTypeID column only, yet record the remaining columns of the row within the system table?
EXEC sys.sp_cdc_enable_table
#source_schema = N'LocationManagementOperational',
#source_name = N'FunctionalLocation',
#role_name = N'Public',
#captured_column_list = N'[ID], [ParentID], [LocationTypeID], [Path], [Elevation], [InclinationPositionX], [InclinationPositionY], [InclinationPercentage], [InclinationAzimuth], [DispatchSignId], [MaterialGeometry], [DynamicDigLimitInclination], [LastUpdate], [RecordStatus]',
#index_name = N'UQ_FunctionalLocation',
#supports_net_changes = 1

SQL Batch Update error - New request is not allowed to start because it should come with valid transaction descriptor

I am running microsoft sql 2008 and jdbc driver 3.0 and am getting this error on a batch SQL update
"New request is not allowed to start because it should come with valid transaction descriptor"
To debug I've reduced the batch size down to just one statement, but it still errors. Here it is
IF EXISTS (SELECT * FROM StaffDetail WHERE PsnID = 'GC91')
UPDATE staffdetail
SET psnid = 'GC91',
servicegroup = '41B001',
discipline = 'IT',
dob = '1967-09-28',
ghdstartdate = '2008-12-15',
yearsexperience = '11 to 20 years',
classification = 'Admin Officer 1'
WHERE psnid = 'GC91'
ELSE
INSERT INTO staffdetail
(psnid,
servicegroup,
discipline,
dob,
ghdstartdate,
yearsexperience,
classification)
VALUES ('GC91',
'41B001',
'IT',
'1967-09-28',
'2008-12-15',
'11 to 20 years',
'Admin Officer 1')
Why does it error? I've googled this and only found references to a bug in SQL 2005 not 2008.
Is there any way I can change the connection string I use to connect or change the SQL statements I use in the batch to try and avoid this error?
Try changing your SQL to this:
UPDATE StaffDetail SET PsnID = 'GC91',ServiceGroup = '41B001',Discipline = 'IT',DOB = '1967-09-28',GHDStartDate = '2008-12-15',YearsExperience = '11 to 20 years',Classification = 'Admin Officer 1' WHERE PsnID = 'GC91';
INSERT INTO StaffDetail (PsnID,ServiceGroup,Discipline,DOB,GHDStartDate,YearsExperience,Classification)
SELECT 'GC91','41B001','IT','1967-09-28','2008-12-15','11 to 20 years','Admin Officer 1'
WHERE NOT EXISTS (SELECT * FROM StaffDetail WHERE PsnID = 'GC91')
can you put the above code as Stored Procedure definition and pass thru using con.prepareStatement ?
A late update, in case others find my findings useful...
I have had this problem a lot recently. Re-architeching the query to either use CTE or Table Variables has often resolved it, but believe that ultimately, as noted at http://computer-aaaargh.blogspot.co.za/2012/12/weird-cause-of-msg-3989-new-request-is.html , it has to do with the underlying data. Look at your joins and try and change what you are fetching back from the underlying data - in this case only the one statement (Try SELECT 1 FROM StaffDetail WHERE PsnID = 'GC91') or use another script in the exist just as a form of elimination.
You can MERGE instead which removes the IF and ELSE which makes it really one statement
MERGE INTO staffdetail AS TARGET
USING (VALUES ('GC91',
'41B001',
'IT',
'1967-09-28',
'2008-12-15',
'11 to 20 years',
'Admin Officer 1')) AS SOURCE (psnid, servicegroup, discipline, dob,
ghdstartdate, yearsexperience, classification)
ON TARGET.psnid = SOURCE.psnid
WHEN MATCHED THEN
UPDATE SET servicegroup = SOURCE.servicegroup,
discipline = SOURCE.discipline,
dob = SOURCE.dob,
ghdstartdate = SOURCE.ghdstartdate,
yearsexperience = SOURCE.yearsexperience,
classification = SOURCE.classification
WHEN NOT MATCHED BY TARGET THEN
INSERT (psnid,
servicegroup,
discipline,
dob,
ghdstartdate,
yearsexperience,
classification)
VALUES ('GC91',
'41B001',
'IT',
'1967-09-28',
'2008-12-15',
'11 to 20 years',
'Admin Officer 1')