Audit Scripts: General Audit
After completing a backup there are a number of things that are good to review prior to restoring into the destination system.
The script below reviews several of the data points that should be looked at prior to restoring.
This script is not exhaustive, but it is a good place to start when looking for anomalies.
- SELECT * FROM
- (
- --Look for custom fields that aren't being used.
- SELECT
- 0 sortOrder,
- '__M_CustomField_Definitions' tableName,
- cfd.Final_Status,
- IIF(COALESCE(cfv.Final_CustomFieldDefinition_Id, '')='', 'Custom Field Not Used', 'Custom Field Used') auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_CustomField_Definitions) totalRows
- FROM
- __M_CustomField_Definitions cfd
- LEFT OUTER JOIN
- (SELECT Final_CustomFieldDefinition_Id FROM __M_CustomField_Values GROUP BY Final_CustomFieldDefinition_Id) cfv
- ON cfd.Id = cfv.Final_CustomFieldDefinition_Id
- GROUP BY
- cfd.Final_Status,
- IIF(COALESCE(cfv.Final_CustomFieldDefinition_Id, '')='', 'Custom Field Not Used', 'Custom Field Used')
- UNION ALL
- -- Look for used/unused practice areas.
- SELECT
- 1 AS sortOrder,
- '__M_Matters_PracticeAreas' AS tableName,
- pa.Final_Status,
- IIF(COALESCE( m.Final_PracticeArea_Id, '') = '', 'Practice Area Not Used', 'Practice Area Used') auditDescription,
- SUM(CASE WHEN m.Final_PracticeArea_Id IS NOT NULL THEN 1 ELSE 0 END) AS numRows,
- (SELECT COUNT(*) FROM __M_Matters_PracticeAreas) AS totalRows
- FROM
- __M_Matters_PracticeAreas pa
- LEFT JOIN
- (SELECT Final_PracticeArea_Id FROM __M_Matters GROUP BY Final_PracticeArea_Id) m
- ON
- pa.Id = m.Final_PracticeArea_Id
- GROUP BY
- pa.Final_Status,
- IIF(COALESCE( m.Final_PracticeArea_Id, '') = '', 'Practice Area Not Used', 'Practice Area Used')
- UNION ALL
- -- Look for contacts with no names
- SELECT
- 2 sortOrder,
- '__M_Contacts' tableName,
- Final_Status,
- IIF( Final_FirstName = '' AND Final_LastName = '' AND Final_FullName = '', 'No Name', 'Has Name') auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Contacts) totalRows
- FROM
- __M_Contacts
- GROUP BY
- Final_Status,
- IIF( Final_FirstName = '' AND Final_LastName = '' AND Final_FullName = '', 'No Name', 'Has Name')
- UNION ALL
- -- Look for Matters missing descriptors
- SELECT
- 2.5 sortOrder,
- '__M_Matters' tableName,
- Final_Status,
- IIF( Final_Subject = '' AND Final_Description = '' AND Final_ReferenceCode = '', 'No Descriptors', 'Has Descriptors') auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Matters) totalRows
- FROM
- __M_Matters
- GROUP BY
- Final_Status,
- IIF( Final_Subject = '' AND Final_Description = '' AND Final_ReferenceCode = '', 'No Descriptors', 'Has Descriptors')
- UNION ALL
- SELECT
- 2.61 sortOrder,
- '__M_Matters' tableName,
- 'All Statuses' Final_Status,
- CONCAT('Max Description Len - ', MAX(LEN(Final_Description))) auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Matters) totalRows
- FROM
- __M_Matters
- UNION ALL
- SELECT
- 2.6 sortOrder,
- '__M_Matters' tableName,
- 'All Statuses' Final_Status,
- CONCAT('Min Description Len - ', MIN(LEN(Final_Description))) auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Matters) totalRows
- FROM
- __M_Matters
- UNION ALL
- SELECT
- 2.62 sortOrder,
- '__M_Matters' tableName,
- 'All Statuses' Final_Status,
- CONCAT('Min ReferenceCode Len - ', MIN(LEN(Final_ReferenceCode))) auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Matters) totalRows
- FROM
- __M_Matters
- UNION ALL
- SELECT
- 2.63 sortOrder,
- '__M_Matters' tableName,
- 'All Statuses' Final_Status,
- CONCAT('Max ReferenceCode Len - ', MAX(LEN(Final_ReferenceCode))) auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Matters) totalRows
- FROM
- __M_Matters
- UNION ALL
- SELECT
- 2.64 sortOrder,
- '__M_Matters' tableName,
- 'All Statuses' Final_Status,
- CONCAT('Min Subject Len - ', MIN(LEN(Final_Subject))) auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Matters) totalRows
- FROM
- __M_Matters
- UNION ALL
- SELECT
- 2.65 sortOrder,
- '__M_Matters' tableName,
- 'All Statuses' Final_Status,
- CONCAT('Max Subject Len - ', Max(LEN(Final_Subject))) auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Matters) totalRows
- FROM
- __M_Matters
- UNION ALL
- -- Look for activities billed/unbilled status
- SELECT
- 3 sortOrder,
- '__M_Financial_Activities_Expenses' tableName,
- Final_Status,
- IIF( Final_ArInvoice_Id = '', 'Unbilled', 'Billed' ) auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Financial_Activities_Expenses) totalRows
- FROM
- __M_Financial_Activities_Expenses
- GROUP BY
- Final_Status,
- IIF( Final_ArInvoice_Id = '', 'Unbilled', 'Billed' )
- UNION ALL
- SELECT
- 3.1 sortOrder,
- '__M_Financial_Activities_Fees' tableName,
- Final_Status,
- IIF( Final_ArInvoice_Id = '', 'Unbilled', 'Billed' ) auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Financial_Activities_Fees) totalRows
- FROM
- __M_Financial_Activities_Fees
- GROUP BY
- Final_Status,
- IIF( Final_ArInvoice_Id = '', 'Unbilled', 'Billed' )
- UNION ALL
- SELECT
- 3.2 sortOrder,
- '__M_Financial_Activities_Expenses' tableName,
- Final_Status,
- IIF( Final_ArInvoice_Id = '', 'Unbilled', 'Billed' ) auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Financial_Activities_Expenses) totalRows
- FROM
- __M_Financial_Activities_Expenses
- GROUP BY
- Final_Status,
- IIF( Final_ArInvoice_Id = '', 'Unbilled', 'Billed' )
- UNION ALL
- --Look at Invoice Outstanding/not Outstanding balance counts.
- SELECT
- 3.3 sortOrder,
- '__M_Financial_AR_Invoices' tableName,
- Final_Status,
- IIF( Final_Outstanding_Override > 0, 'Outstanding Balance', 'No Outstanding Balance') auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Financial_AR_Invoices) totalRows
- FROM
- __M_Financial_AR_Invoices
- GROUP BY
- Final_Status,
- IIF( Final_Outstanding_Override > 0, 'Outstanding Balance', 'No Outstanding Balance')
- UNION ALL
- -- Look for matters missing a contact
- SELECT
- 4 sortOrder,
- '__M_Matters' tableName,
- m.Final_Status,
- IIF(COALESCE(c.Id, '')='', 'Contact Missing', 'Contact Eists') auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Matters) totalRows
- FROM
- __M_Matters m
- LEFT OUTER JOIN
- __M_Contacts c
- ON m.Final_ClientContact_Id = c.Id
- GROUP BY
- m.Final_Status,
- IIF(COALESCE(c.Id, '')='', 'Contact Missing', 'Contact Eists')
- UNION ALL
- -- Look for documents linked to matters with a missing matter reference.
- SELECT
- 5 sortOrder,
- '__M_Documents_Digital_Files' tableName,
- df.Final_Status,
- IIF(COALESCE(m.Id, '')='', 'Matter Missing', 'Matter Exists') auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Documents_Digital_Files) totalRows
- FROM
- __M_Documents_Digital_Files df
- LEFT OUTER JOIN
- __M_Matters m
- ON df.Final_Parent_Id= m.Id
- WHERE 1=1
- AND df.Final_Parent_Type = '__M_Matters'
- GROUP BY
- df.Final_Status,
- IIF(COALESCE(m.Id, '')='', 'Matter Missing', 'Matter Exists')
- UNION ALL
- -- Look for documents linked to Contacts with a missing Contact reference.
- SELECT
- 5.1 sortOrder,
- '__M_Documents_Digital_Files' tableName,
- df.Final_Status,
- IIF(COALESCE(c.Id, '')='', 'Orphan Contacts', 'Contact Exists') auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Documents_Digital_Files) totalRows
- FROM
- __M_Documents_Digital_Files df
- LEFT OUTER JOIN
- __M_Contacts c
- ON df.Final_Parent_Id= c.Id
- WHERE 1=1
- AND df.Final_Parent_Type = '__M_Contacts'
- GROUP BY
- df.Final_Status,
- IIF(COALESCE(c.Id, '')='', 'Orphan Contacts', 'Contact Exists')
- UNION ALL
- -- Look for Documents with/without content paths
- SELECT
- 5.2 sortOrder,
- '__M_Documents_Digital_Files' tableName,
- df.Final_Status,
- IIF(COALESCE(dfv.hasContentPath, 0)=0, 'No Content Path', 'Has Content Path') auditDescription,
- COUNT(*) numRows,
- (SELECT COUNT(*) FROM __M_Documents_Digital_Files) totalRows
- FROM
- __M_Documents_Digital_Files df
- LEFT OUTER JOIN
- (SELECT Final_DocumentFile_Id, SUM(IIF(Final_ContentPath='',0,1)) hasContentPath FROM __M_Documents_Digital_Files_Versions GROUP BY
- Final_DocumentFile_Id) dfv
- ON df.Id = dfv.Final_DocumentFile_Id
- GROUP BY
- df.Final_Status,
- IIF(COALESCE(dfv.hasContentPath, 0)=0, 'No Content Path', 'Has Content Path')
- ) T
- ORDER BY
- sortOrder, tableName, auditDescription
Related Articles
Audit Scripts: Activity Billed/Unbilled row counts
After you have backed up financial information like Time Entries, Expense Entries, and Fee Entries it is a good idea to audit the results prior to restoring data to the destination system. For example, some Universal Destinations support restoring ...
Audit Scripts: Row Counts and Progress Monitoring
This article shows you a script that can be used to monitor the progress of both a Backup and a Restore. It can also be used to just review row counts on all the Universal Database tables. The first code block is used to populate a SQL Server Temp ...
Synthesize Missing Matters for Documents
Some systems only allow documents to be linked to matters. These scripts will allow you to restore data into these systems. When Matters No Longer Exist The following script with synthesize matters for document related to matters that no longer ...
Other: Trimming Field Lengths
Sometimes you may want to manually trim records so that they have certain lengths. The scripts below provide examples on how to best do this. --This is the max length you want DECLARE @MaxLength INT = 50 --This is the characters you want for an ...
Prioritizing documents based on different criteria
The following scripts provide examples that can be modified to apply different priorities to documents. Prioritize Large/Small Documents If you are restoring into a system that has different action limits at different times, this can help you ...