


INNER JOIN corrupt_db.sys.objects o ON o.schema_id = s.schema_idĬROSS APPLY o.object_id, NULL, NULL, 'DETAILED') dpaĪLTER DATABASE corrupt_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE ĭECLARE cur CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY The DBCC WRITEPAGE writes a randomly generated single byte to a randomly chosen location within the page. We then iterate a cursor over the temporary table, calling DBCC WRITEPAGE once against each page. We’re inserting the list of pages used by the table into a temporary table since we won’t be able to directly access the corrupt_db database while using DBCC WRITEPAGE. This next piece of code will thoroughly corrupt the dbo.corrupt_table by placing the database into single user mode, then running DBCC WRITEPAGE to randomly alter a single byte on each page of the table. Database needs to be in single user mode. If you attempt to use “write_direct” with a value of 1, but don’t have the database in single user mode, you’ll receive the following error: Msg 7919, Level 16, State 5, Line 91 The “write_direct” parameter should be 0 if you want DBCC WRITEPAGE to write to a buffer in memory, or 1 if you can put the database into single-user-mode, and have DBCC WRITEPAGE write directly to the data on-disk. The “value” parameter is a hexadecimal value to write into the page the length of this parameter must match the “length” parameter. The “length” parameter indicates how much data we’ll be writing. The “offset” parameter indicates the starting address on the page where we should start writing data.
