3

I have created a stored procedure that uses dbcc checkdb on all userdatabases, if there are any errors detected it then collates the errors and formulates a html table and sends an html email to myself, if there are no errors it sends an email with text along the lines of 'no integrity errors found'.

There are no integrity errors regarding the database and so it does indeed successfully send me an email saying 'no integrity errors found'.

Now I am trying to test the error detection and collation, and to do so I have to create ideally multiple integrity errors but am at a loss as to how to do this on purpose, can this be done?

P.S. I'm using SQL Server 2019

2

1 Answer 1

3

Use DBCC WRITEPAGE:

SELECT
    cmd = 
        N'DBCC WRITEPAGE (' + CAST(dddpa.database_id AS nvarchar(10)) + N', ' +
        CAST(dddpa.allocated_page_file_id AS nvarchar(10)) + N', ' +
        CAST(dddpa.allocated_page_page_id AS nvarchar(10)) + N', ' +
        N'0' + 
        N', ' + 
        N'4' + 
        N', ' + 
        N'0x0000008A' +
        N') WITH NO_INFOMSGS'
FROM sys.dm_db_database_page_allocations
    (
        DB_ID(), 
        NULL, 
        NULL, 
        NULL, 
        'DETAILED'
    ) AS dddpa;
2
  • 1
    Should probably have a health warning?⚠️ “Microsoft does not support the usage of DBCC WRITEPAGE and its use would invalidate the support for a customer database on which it is used.” - though of course hopefully the OP is not trying to introduce corruption on any database other than one dedicated to that anyway Commented Jun 30, 2023 at 13:49
  • Thanks, I ran your statement (on a test db on a test db server) it affect over 900k rows, so imagine my surprise when a ;dbcc checkdb (DBNAME); returned 'CHECKDB found 0 allocation errors and 0 consistency errors in database', but ;dbcc checkdb (wedfut) with no_infomsgs, all_errormsgs, tableresults; gave me over 33 rows of errors!... now to try and sort out my script which isn't collating them correctly!
    – BLoB
    Commented Jun 30, 2023 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.