TSQL Script to Display Agent Job History
It can be a bit time consuming to work through the the SQL Server Management Studio (SSMS) user interface to check on the agent job… Read More »TSQL Script to Display Agent Job History
It can be a bit time consuming to work through the the SQL Server Management Studio (SSMS) user interface to check on the agent job… Read More »TSQL Script to Display Agent Job History
Its not always quick and easy in SQL Server to get a full list of the jobs that have been run, when they were run… Read More »SQL Server Script to Display Job History
The alternate solution to the Database Corruption Challenge this week was created by Patrick Flynn. This solution is the only solution to successfully recover all the data without using any of the backups. If the challenge had been structured differently and all you had was just the database files and no backups, this solution would work just fine.
As you read through this solution, you will notice the use of Common Table Expressions throughout the solution. Patrick is the first person that I have seen write TSQL code that uses Common Table Expressions more than I do. (I mean that as a compliment Patrick)
To start with let’s add the UDF_Convert_Hex_to_Binary function into the master database to match what Patrick had in his system.
All code provided by Patrick, comments and screenshots by me.
-- Step 0 - Create Supporting function USE [master] GO CREATE FUNCTION [dbo].[UDF_Convert_Hex_to_Binary] ( @HEX VARBINARY(MAX) ) RETURNS VARCHAR(MAX) BEGIN DECLARE @BINARY VARCHAR(MAX) ;WITH N1 (n) AS (SELECT 1 UNION ALL SELECT 1), N2 (n) AS (SELECT 1 FROM N1 AS X, N1 AS Y), N3 (n) AS (SELECT 1 FROM N2 AS X, N2 AS Y), N4 (n) AS (SELECT ROW_NUMBER() OVER(ORDER BY X.n) FROM N3 AS X, N3 AS Y) SELECT @BINARY=ISNULL(@BINARY,'') + CONVERT(NVARCHAR(1), (SUBSTRING(@HEX, Nums.n, 1) / 128) % 2) + CONVERT(NVARCHAR(1), (SUBSTRING(@HEX, Nums.n, 1) / 64) % 2) + CONVERT(NVARCHAR(1), (SUBSTRING(@HEX, Nums.n, 1) / 32) % 2) + CONVERT(NVARCHAR(1), (SUBSTRING(@HEX, Nums.n, 1) / 16) % 2) + CONVERT(NVARCHAR(1), (SUBSTRING(@HEX, Nums.n, 1) / 8) % 2) + CONVERT(NVARCHAR(1), (SUBSTRING(@HEX, Nums.n, 1) / 4) % 2) + CONVERT(NVARCHAR(1), (SUBSTRING(@HEX, Nums.n, 1) / 2) % 2) + CONVERT(NVARCHAR(1), SUBSTRING(@HEX, Nums.n, 1) % 2) FROM N4 Nums WHERE Nums.n<=LEN(@HEX) RETURN @BINARY END GO
Once that function is created, let’s drop the previous CorruptionChallenge7 database, because this is probably not our first time running through the solution. If you have a different way of checking for a database and dropping it, please see my blog post related to this topic earlier today.
Read More »Database Corruption Challenge Week 7 – Alternate Solution
Last month I posted another CTE blog entry Using a CTE to Split a String Into Rows with Line Numbers. Since then I have used it… Read More »VARCHAR diff function
Last year while working on my CTE presentation for a SQL Saturday I added a blog post called “Using a CTE to Split a String… Read More »Using a CTE to Split a String Into Rows with Line Numbers