</h1>
<div class="clear"></div>
<div class="postBody">
<div id="cnblogs_post_body" class="blogpost-body blogpost-body-html">
恢复SQL Server被误删除的数据
地址:http://www.cnblogs.com/lyhabc/p/4620764.html
曾经想实现Log Explorer for SQL Server的功能,利用ldf里面的日志来还原误删除的数据
这里有一篇文章做到了,不过似乎不是所有的数据类型都支持
以下为译文:http://raresql.com/2011/10/22/how-to-recover-deleted-data-from-sql-sever/
在我使用SQLSERVER的这些年里面,大部分人都会问我一个问题:“能不能恢复被删除的数据??”
现在,从SQLSERVER2005 或以上版本能很容易能够恢复被删除的数据
(注意:这个脚本能恢复下面的数据类型的数据 而且兼容CS 排序规则)
- image
- text
- uniqueidentifier
- tinyint
- smallint
- int
- smalldatetime
- real
- money
- datetime
- float
- sql_variant
- ntext
- bit
- decimal
- numeric
- smallmoney
- bigint
- varbinary
- varchar
- binary
- char
- timestamp
- nvarchar
- nchar
- xml
- sysname
让我来用demo来解释一下我是怎么做到的
USE master GO --创建数据库 CREATE DATABASE test GO
USE [test]
GO
–创建表
CREATE TABLE [dbo].[aa](
[id] [int] IDENTITY(1,1) NOT NULL,
[NAME] [nvarchar](200) NULL
) ON [PRIMARY]
GO
–插入测试数据
INSERT [dbo].[aa]
( [NAME] )
SELECT ‘你好’
GO
–删除数据
Delete from aa
Go
–验证数据是否已经删除
Select * from aa
Go
现在你需要创建一个存储过程来恢复你的数据
-- Script Name: Recover_Deleted_Data_Proc -- Script Type : Recovery Procedure -- Develop By: Muhammad Imran -- Date Created: 15 Oct 2011 -- Modify Date: 22 Aug 2012 -- Version : 3.1 -- Notes : Included BLOB data types for recovery.& Compatibile with Default , CS collation , Arabic_CI_AS.
CREATE PROCEDURE Recover_Deleted_Data_Proc
@Database_Name NVARCHAR(MAX) ,
@SchemaName_n_TableName NVARCHAR(MAX) ,
@Date_From DATETIME = ‘1900/01/01’ ,
@Date_To DATETIME = ‘9999/12/31’
AS
DECLARE @RowLogContents VARBINARY(8000)
DECLARE @TransactionID NVARCHAR(MAX)
DECLARE @AllocUnitID BIGINT
DECLARE @AllocUnitName NVARCHAR(MAX)
DECLARE @SQL NVARCHAR(MAX)
DECLARE @Compatibility_Level INT
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(0, 128, 0, 1)">@Compatibility_Level</span> <span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)"> dtb.compatibility_level
</span><span style="color: rgba(0, 0, 255, 1)">FROM</span> master.sys.databases <span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> dtb
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> dtb.name <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(0, 128, 0, 1)">@Database_Name</span>
<span style="color: rgba(0, 0, 255, 1)">IF</span> <span style="color: rgba(255, 0, 255, 1)">ISNULL</span>(<span style="color: rgba(0, 128, 0, 1)">@Compatibility_Level</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span>) <span style="color: rgba(128, 128, 128, 1)"><=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>
<span style="color: rgba(0, 0, 255, 1)">BEGIN</span>
<span style="color: rgba(0, 0, 255, 1)">RAISERROR</span>(<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">The compatibility level should be equal to or greater SQL SERVER 2005 (90)</span><span style="color: rgba(255, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1); font-weight: bold">16</span>,<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">RETURN</span>
<span style="color: rgba(0, 0, 255, 1)">END</span>
<span style="color: rgba(0, 0, 255, 1)">IF</span> ( <span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(255, 0, 255, 1)">COUNT</span>(<span style="color: rgba(128, 128, 128, 1)">*</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">FROM</span><span style="color: rgba(0, 0, 0, 1)"> INFORMATION_SCHEMA.TABLES
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">TABLE_SCHEMA</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">+</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">.</span><span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(128, 128, 128, 1)">+</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">TABLE_NAME</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(0, 128, 0, 1)">@SchemaName_n_TableName</span><span style="color: rgba(0, 0, 0, 1)">
) </span><span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span>
<span style="color: rgba(0, 0, 255, 1)">BEGIN</span>
<span style="color: rgba(0, 0, 255, 1)">RAISERROR</span>(<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">Could not found the table in the defined database</span><span style="color: rgba(255, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1); font-weight: bold">16</span>,<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">RETURN</span>
<span style="color: rgba(0, 0, 255, 1)">END</span>
<span style="color: rgba(0, 0, 255, 1)">DECLARE</span> <span style="color: rgba(0, 128, 0, 1)">@bitTable</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)">
(
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Bitvalue</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)">
)
–Create table to set the bit position of one byte.
<span style="color: rgba(0, 0, 255, 1)">INSERT</span> <span style="color: rgba(0, 0, 255, 1)">INTO</span> <span style="color: rgba(0, 128, 0, 1)">@bitTable</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span>
<span style="color: rgba(0, 0, 255, 1)">UNION</span> <span style="color: rgba(128, 128, 128, 1)">ALL</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span>
<span style="color: rgba(0, 0, 255, 1)">UNION</span> <span style="color: rgba(128, 128, 128, 1)">ALL</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">4</span>
<span style="color: rgba(0, 0, 255, 1)">UNION</span> <span style="color: rgba(128, 128, 128, 1)">ALL</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">8</span>
<span style="color: rgba(0, 0, 255, 1)">UNION</span> <span style="color: rgba(128, 128, 128, 1)">ALL</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">4</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">16</span>
<span style="color: rgba(0, 0, 255, 1)">UNION</span> <span style="color: rgba(128, 128, 128, 1)">ALL</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">5</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">32</span>
<span style="color: rgba(0, 0, 255, 1)">UNION</span> <span style="color: rgba(128, 128, 128, 1)">ALL</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">6</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">64</span>
<span style="color: rgba(0, 0, 255, 1)">UNION</span> <span style="color: rgba(128, 128, 128, 1)">ALL</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">7</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">128</span>
–Create table to collect the row data.
DECLARE @DeletedRecords TABLE
(
[Row ID] INT IDENTITY(1, 1) ,
[RowLogContents] VARBINARY(8000) ,
[AllocUnitID] BIGINT ,
[Transaction ID] NVARCHAR(MAX) ,
[FixedLengthData] SMALLINT ,
[TotalNoOfCols] SMALLINT ,
[NullBitMapLength] SMALLINT ,
[NullBytes] VARBINARY(8000) ,
[TotalNoofVarCols] SMALLINT ,
[ColumnOffsetArray] VARBINARY(8000) ,
[VarColumnStart] SMALLINT ,
[Slot ID] INT ,
[NullBitMap] VARCHAR(MAX)
)
–Create a common table expression to get all the row data plus how many bytes we have for each row.
;
WITH RowData
AS ( SELECT [RowLog Contents 0] AS [RowLogContents] ,
[AllocUnitID] AS [AllocUnitID] ,
[Transaction ID] AS [Transaction ID]
–[Fixed Length Data] = Substring (RowLog content 0, Status Bit A+ Status Bit B + 1,2 bytes)
,
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) AS [FixedLengthData] –@FixedLengthData
– [TotalnoOfCols] = Substring (RowLog content 0, [Fixed Length Data] + 1,2 bytes)
,
CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 1,
2)))) AS [TotalNoOfCols]
–[NullBitMapLength]=ceiling([Total No of Columns] /8.0)
,
CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 1,
2)))) / 8.0)) AS [NullBitMapLength]
–[Null Bytes] = Substring (RowLog content 0, Status Bit A+ Status Bit B + [Fixed Length Data] +1, [NullBitMapLength] )
,
SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 3,
CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 1,
2)))) / 8.0))) AS [NullBytes]
–[TotalNoofVarCols] = Substring (RowLog content 0, Status Bit A+ Status Bit B + [Fixed Length Data] +1, [Null Bitmap length] + 2 )
,
( CASE WHEN SUBSTRING([RowLog Contents 0], 1, 1) IN (
0x10, 0x30, 0x70 )
THEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 3
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 1,
2)))) / 8.0)), 2))))
ELSE NULL
END ) AS [TotalNoofVarCols]
–[ColumnOffsetArray]= Substring (RowLog content 0, Status Bit A+ Status Bit B + [Fixed Length Data] +1, [Null Bitmap length] + 2 , [TotalNoofVarCols]2 )
,
( CASE WHEN SUBSTRING([RowLog Contents 0], 1, 1) IN (
0x10, 0x30, 0x70 )
THEN SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 3
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 1,
2)))) / 8.0))
+ 2,
( CASE WHEN SUBSTRING([RowLog Contents 0],
1, 1) IN ( 0x10,
0x30, 0x70 )
THEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 3
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 1,
2)))) / 8.0)), 2))))
ELSE NULL
END ) 2)
ELSE NULL
END ) AS [ColumnOffsetArray]
– Variable column Start = Status Bit A+ Status Bit B + [Fixed Length Data] + [Null Bitmap length] + 2+([TotalNoofVarCols]2)
,
CASE WHEN SUBSTRING([RowLog Contents 0], 1, 1) IN (
0x10, 0x30, 0x70 )
THEN ( CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 4
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 1,
2)))) / 8.0))
+ ( ( CASE WHEN SUBSTRING([RowLog Contents 0],
1, 1) IN ( 0x10,
0x30, 0x70 )
THEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 3
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
CONVERT(SMALLINT, CONVERT(BINARY(2), REVERSE(SUBSTRING([RowLog Contents 0],
2 + 1, 2)))) + 1,
2)))) / 8.0)), 2))))
ELSE NULL
END ) 2 ) )
ELSE NULL
END AS [VarColumnStart] ,
[Slot ID]
FROM sys.fn_dblog(NULL, NULL)
WHERE AllocUnitId IN (
SELECT [Allocation_unit_id]
FROM sys.allocation_units allocunits
INNER JOIN sys.partitions partitions ON ( allocunits.type IN (
1, 3 )
AND partitions.hobt_id = allocunits.container_id
)
OR ( allocunits.type = 2
AND partitions.partition_id = allocunits.container_id
)
WHERE object_id = OBJECT_ID(‘’
+ @SchemaName_n_TableName
+ ‘’) )
AND Context IN ( ‘LCX_MARK_AS_GHOST’, ‘LCX_HEAP’ )
AND Operation IN ( ‘LOP_DELETE_ROWS’ )
AND SUBSTRING([RowLog Contents 0], 1, 1) IN ( 0x10,
0x30, 0x70 )
/Use this subquery to filter the date/
AND [TRANSACTION ID] IN (
SELECT DISTINCT
[TRANSACTION ID]
FROM sys.fn_dblog(NULL, NULL)
WHERE Context IN ( ‘LCX_NULL’ )
AND Operation IN ( ‘LOP_BEGIN_XACT’ )
AND [Transaction Name] IN ( ‘DELETE’,
‘user_transaction’ )
AND CONVERT(NVARCHAR(11), [Begin Time]) BETWEEN @Date_From
AND
@Date_To )
),
–Use this technique to repeate the row till the no of bytes of the row.
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
)
INSERT INTO @DeletedRecords
SELECT RowLogContents ,
[AllocUnitID] ,
[Transaction ID] ,
[FixedLengthData] ,
[TotalNoOfCols] ,
[NullBitMapLength] ,
[NullBytes] ,
[TotalNoofVarCols] ,
[ColumnOffsetArray] ,
[VarColumnStart] ,
[Slot ID]
–-Get the Null value against each column (1 means null zero means not null)
,
[NullBitMap] = ( REPLACE(STUFF(( SELECT
‘,’
+ ( CASE
WHEN [ID] = 0
THEN CONVERT(NVARCHAR(1), ( SUBSTRING(NullBytes,
n, 1) % 2 ))
ELSE CONVERT(NVARCHAR(1), ( ( SUBSTRING(NullBytes,
n, 1)
/ [Bitvalue] )
% 2 ))
END ) –as [nullBitMap]
FROM N4 AS Nums
JOIN RowData AS C ON n <= NullBitMapLength
CROSS JOIN @bitTable
WHERE
C.[RowLogContents] = D.[RowLogContents]
ORDER BY [RowLogContents] ,
n ASC
FOR
XML PATH(‘’)
), 1, 1, ‘’), ‘,’, ‘’) )
FROM RowData D
</span><span style="color: rgba(0, 0, 255, 1)">IF</span> ( <span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(255, 0, 255, 1)">COUNT</span>(<span style="color: rgba(128, 128, 128, 1)">*</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">FROM</span> <span style="color: rgba(0, 128, 0, 1)">@DeletedRecords</span><span style="color: rgba(0, 0, 0, 1)">
) </span><span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span>
<span style="color: rgba(0, 0, 255, 1)">BEGIN</span>
<span style="color: rgba(0, 0, 255, 1)">RAISERROR</span>(<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">There is no data in the log as per the search criteria</span><span style="color: rgba(255, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1); font-weight: bold">16</span>,<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">RETURN</span>
<span style="color: rgba(0, 0, 255, 1)">END</span>
<span style="color: rgba(0, 0, 255, 1)">DECLARE</span> <span style="color: rgba(0, 128, 0, 1)">@ColumnNameAndData</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)">
(
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Row ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Rowlogcontents</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">VARBINARY</span>(<span style="color: rgba(255, 0, 255, 1)">MAX</span><span style="color: rgba(0, 0, 0, 1)">) ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">NAME</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">nullbit</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">SMALLINT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">leaf_offset</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">SMALLINT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">length</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">SMALLINT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">system_type_id</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">TINYINT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">bitpos</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">TINYINT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">xprec</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">TINYINT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">xscale</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">TINYINT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">is_null</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Column value Size</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Column Length</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">hex_Value</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">VARBINARY</span>(<span style="color: rgba(255, 0, 255, 1)">MAX</span><span style="color: rgba(0, 0, 0, 1)">) ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Slot ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Update</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)">
)
–Create common table expression and join it with the rowdata table
– to get each column details
/This part is for variable data columns/
–@RowLogContents,
–(col.columnOffValue - col.columnLength) + 1,
–col.columnLength
–)
INSERT INTO @ColumnNameAndData
SELECT [Row ID] ,
Rowlogcontents ,
NAME ,
cols.leaf_null_bit AS nullbit ,
leaf_offset ,
ISNULL(syscolumns.length, cols.max_length) AS [length] ,
cols.system_type_id ,
cols.leaf_bit_position AS bitpos ,
ISNULL(syscolumns.xprec, cols.precision) AS xprec ,
ISNULL(syscolumns.xscale, cols.scale) AS xscale ,
SUBSTRING([nullBitMap], cols.leaf_null_bit, 1) AS is_null ,
( CASE WHEN leaf_offset < 1
AND SUBSTRING([nullBitMap], cols.leaf_null_bit,
1) = 0
THEN ( CASE WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) > 30000
THEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
- POWER(2, 15)
ELSE CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
END )
END ) AS [Column value Size] ,
( CASE WHEN leaf_offset < 1
AND SUBSTRING([nullBitMap], cols.leaf_null_bit,
1) = 0
THEN ( CASE WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) > 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) < 30000
THEN ( CASE WHEN [System_type_id] IN (
35, 34, 99 ) THEN 16
ELSE 24
END )
WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) > 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) > 30000
THEN ( CASE WHEN [System_type_id] IN (
35, 34, 99 ) THEN 16
ELSE 24
END ) –24
WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) < 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) < 30000
THEN ( CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) )
WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) < 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) > 30000
THEN POWER(2, 15)
+ CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart])
END )
END ) AS [Column Length] ,
( CASE WHEN SUBSTRING([nullBitMap], cols.leaf_null_bit, 1) = 1
THEN NULL
ELSE SUBSTRING(Rowlogcontents,
( ( CASE WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) > 30000
THEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
- POWER(2, 15)
ELSE CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
END )
- ( CASE WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) > 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) < 30000
THEN ( CASE
WHEN [System_type_id] IN (
35, 34, 99 )
THEN 16
ELSE 24
END ) –24
WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) > 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) > 30000
THEN ( CASE
WHEN [System_type_id] IN (
35, 34, 99 )
THEN 16
ELSE 24
END ) –24
WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) < 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) < 30000
THEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart])
WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) < 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) > 30000
THEN POWER(2, 15)
+ CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart])
END ) ) + 1,
( CASE WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) > 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) < 30000
THEN ( CASE WHEN [System_type_id] IN (
35, 34, 99 )
THEN 16
ELSE 24
END ) –24
WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) > 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) > 30000
THEN ( CASE WHEN [System_type_id] IN (
35, 34, 99 )
THEN 16
ELSE 24
END ) –24
WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) < 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) < 30000
THEN ABS(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]))
WHEN CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2)))) < 30000
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart]) > 30000
THEN POWER(2, 15)
+ CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
leaf_offset
-1 ) - 1, 2))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(2), REVERSE(SUBSTRING([ColumnOffsetArray],
( 2
( ( leaf_offset
-1 ) - 1 ) )
- 1, 2)))), 0),
[varColumnStart])
END ))
END ) AS hex_Value ,
[Slot ID] ,
0
FROM @DeletedRecords A
INNER JOIN sys.allocation_units allocunits ON A.[AllocUnitId] = allocunits.[Allocation_Unit_Id]
INNER JOIN sys.partitions partitions ON ( allocunits.type IN (
1, 3 )
AND partitions.hobt_id = allocunits.container_id
)
OR ( allocunits.type = 2
AND partitions.partition_id = allocunits.container_id
)
INNER JOIN sys.system_internals_partition_columns cols ON cols.partition_id = partitions.partition_id
LEFT OUTER JOIN syscolumns ON syscolumns.id = partitions.object_id
AND syscolumns.colid = cols.partition_column_id
WHERE leaf_offset < 0
UNION
/This part is for fixed data columns/
SELECT [Row ID] ,
Rowlogcontents ,
NAME ,
cols.leaf_null_bit AS nullbit ,
leaf_offset ,
ISNULL(syscolumns.length, cols.max_length) AS [length] ,
cols.system_type_id ,
cols.leaf_bit_position AS bitpos ,
ISNULL(syscolumns.xprec, cols.precision) AS xprec ,
ISNULL(syscolumns.xscale, cols.scale) AS xscale ,
SUBSTRING([nullBitMap], cols.leaf_null_bit, 1) AS is_null ,
( SELECT TOP 1
ISNULL(SUM(CASE WHEN C.leaf_offset > 1
THEN max_length
ELSE 0
END), 0)
FROM sys.system_internals_partition_columns C
WHERE cols.partition_id = C.partition_id
AND C.leaf_null_bit < cols.leaf_null_bit
) + 5 AS [Column value Size] ,
syscolumns.length AS [Column Length] ,
CASE WHEN SUBSTRING([nullBitMap], cols.leaf_null_bit, 1) = 1
THEN NULL
ELSE SUBSTRING(Rowlogcontents,
( SELECT TOP 1
ISNULL(SUM(CASE
WHEN C.leaf_offset > 1
AND C.leaf_bit_position = 0
THEN max_length
ELSE 0
END), 0)
FROM sys.system_internals_partition_columns C
WHERE cols.partition_id = C.partition_id
AND C.leaf_null_bit < cols.leaf_null_bit
) + 5, syscolumns.length)
END AS hex_Value ,
[Slot ID] ,
0
FROM @DeletedRecords A
INNER JOIN sys.allocation_units allocunits ON A.[AllocUnitId] = allocunits.[Allocation_Unit_Id]
INNER JOIN sys.partitions partitions ON ( allocunits.type IN (
1, 3 )
AND partitions.hobt_id = allocunits.container_id
)
OR ( allocunits.type = 2
AND partitions.partition_id = allocunits.container_id
)
INNER JOIN sys.system_internals_partition_columns cols ON cols.partition_id = partitions.partition_id
LEFT OUTER JOIN syscolumns ON syscolumns.id = partitions.object_id
AND syscolumns.colid = cols.partition_column_id
WHERE leaf_offset > 0
ORDER BY nullbit
</span><span style="color: rgba(0, 0, 255, 1)">DECLARE</span> <span style="color: rgba(0, 128, 0, 1)">@BitColumnByte</span> <span style="color: rgba(0, 0, 255, 1)">AS</span> <span style="color: rgba(0, 0, 255, 1)">INT</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(0, 128, 0, 1)">@BitColumnByte</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 255, 1)">CONVERT</span>(<span style="color: rgba(0, 0, 255, 1)">INT</span>, <span style="color: rgba(255, 0, 255, 1)">CEILING</span>(<span style="color: rgba(255, 0, 255, 1)">COUNT</span>(<span style="color: rgba(128, 128, 128, 1)">*</span>) <span style="color: rgba(128, 128, 128, 1)">/</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">8.0</span><span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 0, 255, 1)">FROM</span> <span style="color: rgba(0, 128, 0, 1)">@ColumnNameAndData</span>
<span style="color: rgba(0, 0, 255, 1)">WHERE</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">System_Type_id</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">104</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">WITH</span><span style="color: rgba(0, 0, 0, 1)"> N1 ( n )
</span><span style="color: rgba(0, 0, 255, 1)">AS</span> ( <span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>
<span style="color: rgba(0, 0, 255, 1)">UNION</span> <span style="color: rgba(128, 128, 128, 1)">ALL</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">
),
N2 ( n )
</span><span style="color: rgba(0, 0, 255, 1)">AS</span> ( <span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span> N1 <span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> X ,
N1 </span><span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> Y
),
N3 ( n )
</span><span style="color: rgba(0, 0, 255, 1)">AS</span> ( <span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span> N2 <span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> X ,
N2 </span><span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> Y
),
N4 ( n )
</span><span style="color: rgba(0, 0, 255, 1)">AS</span> ( <span style="color: rgba(0, 0, 255, 1)">SELECT</span> ROW_NUMBER() <span style="color: rgba(0, 0, 255, 1)">OVER</span> ( <span style="color: rgba(0, 0, 255, 1)">ORDER</span> <span style="color: rgba(0, 0, 255, 1)">BY</span><span style="color: rgba(0, 0, 0, 1)"> X.n )
</span><span style="color: rgba(0, 0, 255, 1)">FROM</span> N3 <span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> X ,
N3 </span><span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> Y
),
CTE
</span><span style="color: rgba(0, 0, 255, 1)">AS</span> ( <span style="color: rgba(0, 0, 255, 1)">SELECT</span><span style="color: rgba(0, 0, 0, 1)"> RowLogContents ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">nullbit</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">BitMap</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 255, 1)">CONVERT</span>(<span style="color: rgba(0, 0, 255, 1)">VARBINARY</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>), <span style="color: rgba(255, 0, 255, 1)">CONVERT</span>(<span style="color: rgba(0, 0, 255, 1)">INT</span>, <span style="color: rgba(255, 0, 255, 1)">SUBSTRING</span>(( <span style="color: rgba(255, 0, 255, 1)">REPLACE</span>(<span style="color: rgba(255, 0, 255, 1)">STUFF</span>(( <span style="color: rgba(0, 0, 255, 1)">SELECT</span>
<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">,</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(128, 128, 128, 1)">+</span> ( <span style="color: rgba(255, 0, 255, 1)">CASE</span>
<span style="color: rgba(0, 0, 255, 1)">WHEN</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span>
<span style="color: rgba(0, 0, 255, 1)">THEN</span> <span style="color: rgba(255, 0, 255, 1)">CONVERT</span>(<span style="color: rgba(0, 0, 255, 1)">NVARCHAR</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>), ( <span style="color: rgba(255, 0, 255, 1)">SUBSTRING</span><span style="color: rgba(0, 0, 0, 1)">(hex_Value,
n, </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>) <span style="color: rgba(128, 128, 128, 1)">%</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)"> ))
</span><span style="color: rgba(0, 0, 255, 1)">ELSE</span> <span style="color: rgba(255, 0, 255, 1)">CONVERT</span>(<span style="color: rgba(0, 0, 255, 1)">NVARCHAR</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>), ( ( <span style="color: rgba(255, 0, 255, 1)">SUBSTRING</span><span style="color: rgba(0, 0, 0, 1)">(hex_Value,
n, </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(128, 128, 128, 1)">/</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Bitvalue</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">%</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)"> ))
</span><span style="color: rgba(0, 0, 255, 1)">END</span> ) <span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">as [nullBitMap]</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span><span style="color: rgba(0, 0, 0, 1)">
N4 </span><span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> Nums
</span><span style="color: rgba(128, 128, 128, 1)">JOIN</span> <span style="color: rgba(0, 128, 0, 1)">@ColumnNameAndData</span>
<span style="color: rgba(0, 0, 255, 1)">AS</span> C <span style="color: rgba(0, 0, 255, 1)">ON</span> n <span style="color: rgba(128, 128, 128, 1)"><=</span> <span style="color: rgba(0, 128, 0, 1)">@BitColumnByte</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">System_Type_id</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">104</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> bitpos <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span>
<span style="color: rgba(0, 0, 255, 1)">CROSS</span> <span style="color: rgba(128, 128, 128, 1)">JOIN</span> <span style="color: rgba(0, 128, 0, 1)">@bitTable</span>
<span style="color: rgba(0, 0, 255, 1)">WHERE</span><span style="color: rgba(0, 0, 0, 1)">
C.</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLogContents</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> D.<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLogContents</span><span style="color: rgba(255, 0, 0, 1)">]</span>
<span style="color: rgba(0, 0, 255, 1)">ORDER</span> <span style="color: rgba(0, 0, 255, 1)">BY</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLogContents</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
n </span><span style="color: rgba(0, 0, 255, 1)">ASC</span>
<span style="color: rgba(0, 0, 255, 1)">FOR</span><span style="color: rgba(0, 0, 0, 1)">
XML
PATH(</span><span style="color: rgba(255, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">)
), </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, <span style="color: rgba(255, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">),
</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">,</span><span style="color: rgba(255, 0, 0, 1)">'</span>, <span style="color: rgba(255, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">) ),
bitpos </span><span style="color: rgba(128, 128, 128, 1)">+</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">)))
</span><span style="color: rgba(0, 0, 255, 1)">FROM</span> <span style="color: rgba(0, 128, 0, 1)">@ColumnNameAndData</span><span style="color: rgba(0, 0, 0, 1)"> D
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">System_Type_id</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">104</span><span style="color: rgba(0, 0, 0, 1)">
)
</span><span style="color: rgba(0, 0, 255, 1)">UPDATE</span><span style="color: rgba(0, 0, 0, 1)"> A
</span><span style="color: rgba(0, 0, 255, 1)">SET</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">hex_Value</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">BitMap</span><span style="color: rgba(255, 0, 0, 1)">]</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span> <span style="color: rgba(0, 128, 0, 1)">@ColumnNameAndData</span><span style="color: rgba(0, 0, 0, 1)"> A
</span><span style="color: rgba(0, 0, 255, 1)">INNER</span> <span style="color: rgba(128, 128, 128, 1)">JOIN</span> CTE B <span style="color: rgba(0, 0, 255, 1)">ON</span> A.<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLogContents</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> B.<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLogContents</span><span style="color: rgba(255, 0, 0, 1)">]</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> A.<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">nullbit</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> B.<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">nullbit</span><span style="color: rgba(255, 0, 0, 1)">]</span>
/Check for BLOB DATA TYPES****************/
DECLARE @Fileid INT
DECLARE @Pageid INT
DECLARE @Slotid INT
DECLARE @CurrentLSN INT
DECLARE @LinkID INT
DECLARE @Context VARCHAR(50)
DECLARE @ConsolidatedPageID VARCHAR(MAX)
DECLARE @LCX_TEXT_MIX VARBINARY(MAX)
</span><span style="color: rgba(0, 0, 255, 1)">DECLARE</span> <span style="color: rgba(0, 128, 0, 1)">@temppagedata</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)">
(
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">ParentObject</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Object</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Field</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Value</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME
)
</span><span style="color: rgba(0, 0, 255, 1)">DECLARE</span> <span style="color: rgba(0, 128, 0, 1)">@pagedata</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)">
(
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Page ID</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">File IDS</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Page IDS</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">AllocUnitId</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">BIGINT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">ParentObject</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Object</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Field</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Value</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> SYSNAME
)
</span><span style="color: rgba(0, 0, 255, 1)">DECLARE</span> <span style="color: rgba(0, 128, 0, 1)">@ModifiedRawData</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)">
(
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span> <span style="color: rgba(255, 0, 255, 1)">IDENTITY</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">) ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">PAGE ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">VARCHAR</span>(<span style="color: rgba(255, 0, 255, 1)">MAX</span><span style="color: rgba(0, 0, 0, 1)">) ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">FILE IDS</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">PAGE IDS</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Slot ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">AllocUnitId</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">BIGINT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Contents 0_var</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">VARCHAR</span>(<span style="color: rgba(255, 0, 255, 1)">MAX</span><span style="color: rgba(0, 0, 0, 1)">) ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Length</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">VARCHAR</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">50</span><span style="color: rgba(0, 0, 0, 1)">) ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Len</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Contents 0</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">VARBINARY</span>(<span style="color: rgba(255, 0, 255, 1)">MAX</span><span style="color: rgba(0, 0, 0, 1)">) ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Link ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span> <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> ( <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span><span style="color: rgba(0, 0, 0, 1)"> ) ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Update</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(0, 0, 255, 1)">INT</span><span style="color: rgba(0, 0, 0, 1)">
)
</span><span style="color: rgba(0, 0, 255, 1)">DECLARE</span> Page_Data_Cursor <span style="color: rgba(0, 0, 255, 1)">CURSOR</span>
<span style="color: rgba(0, 0, 255, 1)">FOR</span>
<span style="color: rgba(0, 128, 128, 1)">/*</span><span style="color: rgba(0, 128, 128, 1)">We need to filter LOP_MODIFY_ROW,LOP_MODIFY_COLUMNS from log for deleted records of BLOB data type& Get its Slot No, Page ID & AllocUnit ID</span><span style="color: rgba(0, 128, 128, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(255, 0, 255, 1)">LTRIM</span>(<span style="color: rgba(255, 0, 255, 1)">RTRIM</span>(<span style="color: rgba(255, 0, 255, 1)">REPLACE</span>(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Description</span><span style="color: rgba(255, 0, 0, 1)">]</span>, <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">Deallocated</span><span style="color: rgba(255, 0, 0, 1)">'</span>, <span style="color: rgba(255, 0, 0, 1)">''</span>))) <span style="color: rgba(0, 0, 255, 1)">AS</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">PAGE ID</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Slot ID</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">AllocUnitId</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(0, 0, 255, 1)">NULL</span> <span style="color: rgba(0, 0, 255, 1)">AS</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Contents 0</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(0, 0, 255, 1)">NULL</span> <span style="color: rgba(0, 0, 255, 1)">AS</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Contents 0</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
Context
</span><span style="color: rgba(0, 0, 255, 1)">FROM</span> sys.fn_dblog(<span style="color: rgba(0, 0, 255, 1)">NULL</span>, <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> AllocUnitId <span style="color: rgba(128, 128, 128, 1)">IN</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Allocation_unit_id</span><span style="color: rgba(255, 0, 0, 1)">]</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span><span style="color: rgba(0, 0, 0, 1)"> sys.allocation_units allocunits
</span><span style="color: rgba(0, 0, 255, 1)">INNER</span> <span style="color: rgba(128, 128, 128, 1)">JOIN</span> sys.partitions partitions <span style="color: rgba(0, 0, 255, 1)">ON</span> ( allocunits.type <span style="color: rgba(128, 128, 128, 1)">IN</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> partitions.hobt_id <span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)"> allocunits.container_id
)
</span><span style="color: rgba(128, 128, 128, 1)">OR</span> ( allocunits.type <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> partitions.partition_id <span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)"> allocunits.container_id
)
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> <span style="color: rgba(255, 0, 255, 1)">object_id</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 255, 1)">OBJECT_ID</span>(<span style="color: rgba(255, 0, 0, 1)">''</span> <span style="color: rgba(128, 128, 128, 1)">+</span> <span style="color: rgba(0, 128, 0, 1)">@SchemaName_n_TableName</span>
<span style="color: rgba(128, 128, 128, 1)">+</span> <span style="color: rgba(255, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">) )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> Operation <span style="color: rgba(128, 128, 128, 1)">IN</span> ( <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">LOP_MODIFY_ROW</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Context</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">IN</span> ( <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">LCX_PFS</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> Description <span style="color: rgba(128, 128, 128, 1)">LIKE</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">%Deallocated%</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">/*</span><span style="color: rgba(0, 128, 128, 1)">Use this subquery to filter the date</span><span style="color: rgba(0, 128, 128, 1)">*/</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">TRANSACTION ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">IN</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(0, 0, 255, 1)">DISTINCT</span>
<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">TRANSACTION ID</span><span style="color: rgba(255, 0, 0, 1)">]</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span> sys.fn_dblog(<span style="color: rgba(0, 0, 255, 1)">NULL</span>, <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> Context <span style="color: rgba(128, 128, 128, 1)">IN</span> ( <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">LCX_NULL</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> Operation <span style="color: rgba(128, 128, 128, 1)">IN</span> ( <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">LOP_BEGIN_XACT</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Transaction Name</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">DELETE</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> <span style="color: rgba(255, 0, 255, 1)">CONVERT</span>(<span style="color: rgba(0, 0, 255, 1)">NVARCHAR</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">11</span>), <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Begin Time</span><span style="color: rgba(255, 0, 0, 1)">]</span>) <span style="color: rgba(128, 128, 128, 1)">BETWEEN</span> <span style="color: rgba(0, 128, 0, 1)">@Date_From</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span>
<span style="color: rgba(0, 128, 0, 1)">@Date_To</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(0, 0, 255, 1)">GROUP</span> <span style="color: rgba(0, 0, 255, 1)">BY</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Description</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Slot ID</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">AllocUnitId</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
Context
</span><span style="color: rgba(0, 0, 255, 1)">UNION</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">PAGE ID</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Slot ID</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">AllocUnitId</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 255, 1)">SUBSTRING</span>(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Contents 0</span><span style="color: rgba(255, 0, 0, 1)">]</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">15</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(255, 0, 255, 1)">LEN</span>(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Contents 0</span><span style="color: rgba(255, 0, 0, 1)">]</span>)) <span style="color: rgba(0, 0, 255, 1)">AS</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Contents 0</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> ,
</span><span style="color: rgba(255, 0, 255, 1)">CONVERT</span>(<span style="color: rgba(0, 0, 255, 1)">INT</span>, <span style="color: rgba(255, 0, 255, 1)">SUBSTRING</span>(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">RowLog Contents 0</span><span style="color: rgba(255, 0, 0, 1)">]</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">7</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">)) ,
Context </span><span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">,CAST(RIGHT([Current LSN],4) AS INT) AS [Current LSN]</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span> sys.fn_dblog(<span style="color: rgba(0, 0, 255, 1)">NULL</span>, <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> AllocUnitId <span style="color: rgba(128, 128, 128, 1)">IN</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Allocation_unit_id</span><span style="color: rgba(255, 0, 0, 1)">]</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span><span style="color: rgba(0, 0, 0, 1)"> sys.allocation_units allocunits
</span><span style="color: rgba(0, 0, 255, 1)">INNER</span> <span style="color: rgba(128, 128, 128, 1)">JOIN</span> sys.partitions partitions <span style="color: rgba(0, 0, 255, 1)">ON</span> ( allocunits.type <span style="color: rgba(128, 128, 128, 1)">IN</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> partitions.hobt_id <span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)"> allocunits.container_id
)
</span><span style="color: rgba(128, 128, 128, 1)">OR</span> ( allocunits.type <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> partitions.partition_id <span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)"> allocunits.container_id
)
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> <span style="color: rgba(255, 0, 255, 1)">object_id</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 255, 1)">OBJECT_ID</span>(<span style="color: rgba(255, 0, 0, 1)">''</span> <span style="color: rgba(128, 128, 128, 1)">+</span> <span style="color: rgba(0, 128, 0, 1)">@SchemaName_n_TableName</span>
<span style="color: rgba(128, 128, 128, 1)">+</span> <span style="color: rgba(255, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">) )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> Context <span style="color: rgba(128, 128, 128, 1)">IN</span> ( <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">LCX_TEXT_MIX</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> Operation <span style="color: rgba(128, 128, 128, 1)">IN</span> ( <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">LOP_DELETE_ROWS</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(0, 128, 128, 1)">/*</span><span style="color: rgba(0, 128, 128, 1)">Use this subquery to filter the date</span><span style="color: rgba(0, 128, 128, 1)">*/</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">TRANSACTION ID</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">IN</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(0, 0, 255, 1)">DISTINCT</span>
<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">TRANSACTION ID</span><span style="color: rgba(255, 0, 0, 1)">]</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span> sys.fn_dblog(<span style="color: rgba(0, 0, 255, 1)">NULL</span>, <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> Context <span style="color: rgba(128, 128, 128, 1)">IN</span> ( <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">LCX_NULL</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> Operation <span style="color: rgba(128, 128, 128, 1)">IN</span> ( <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">LOP_BEGIN_XACT</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(128, 128, 128, 1)">AND</span> <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Transaction Name</span><span style="color: rgba(255, 0, 0, 1)">]</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">DELETE</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> <span style="color: rgba(255, 0, 255, 1)">CONVERT</span>(<span style="color: rgba(0, 0, 255, 1)">NVARCHAR</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">11</span>), <span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">Begin Time</span><span style="color: rgba(255, 0, 0, 1)">]</span>) <span style="color: rgba(128, 128, 128, 1)">BETWEEN</span> <span style="color: rgba(0, 128, 0, 1)">@Date_From</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span>
<span style="color: rgba(0, 128, 0, 1)">@Date_To</span><span style="color: rgba(0, 0, 0, 1)"> )
</span><span style="color: rgba(0, 128, 128, 1)">/*</span><span style="color: rgba(0, 128, 128, 1)">**************************************</span><span style="color: rgba(0, 128, 128, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">OPEN</span><span style="color: rgba(0, 0, 0, 1)"> Page_Data_Cursor
</span><span style="color: rgba(0, 0, 255, 1)">FETCH</span> <span style="color: rgba(0, 0, 255, 1)">NEXT</span> <span style="color: rgba(0, 0, 255, 1)">FROM</span> Page_Data_Cursor <span style="color: rgba(0, 0, 255, 1)">INTO</span> <span style="color: rgba(0, 128, 0, 1)">@ConsolidatedPageID</span>, <span style="color: rgba(0, 128, 0, 1)">@Slotid</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">@AllocUnitID</span>, <span style="color: rgba(0, 128, 0, 1)">@LCX_TEXT_MIX</span>, <span style="color: rgba(0, 128, 0, 1)">@LinkID</span>, <span style="color: rgba(0, 128, 0, 1)">@Context</span>
<span style="color: rgba(0, 0, 255, 1)">WHILE</span> <span style="color: rgba(0, 128, 0, 1); font-weight: bold">@@FETCH_STATUS</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span>
<span style="color: rgba(0, 0, 255, 1)">BEGIN</span>
<span style="color: rgba(0, 0, 255, 1)">DECLARE</span> <span style="color: rgba(0, 128, 0, 1)">@hex_pageid</span> <span style="color: rgba(0, 0, 255, 1)">AS</span> <span style="color: rgba(0, 0, 255, 1)">VARCHAR</span>(<span style="color: rgba(255, 0, 255, 1)">MAX</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">/*</span><span style="color: rgba(0, 128, 128, 1)">Page ID contains File Number and page number It looks like 0001:00000130.
In this example 0001 is file Number & 00000130 is Page Number & These numbers are in Hex format</span><span style="color: rgba(0, 128, 128, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">SET</span> <span style="color: rgba(0, 128, 0, 1)">@Fileid</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 255, 1)">SUBSTRING</span>(<span style="color: rgba(0, 128, 0, 1)">@ConsolidatedPageID</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(255, 0, 255, 1)">CHARINDEX</span>(<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">:</span><span style="color: rgba(255, 0, 0, 1)">'</span>, <span style="color: rgba(0, 128, 0, 1)">@ConsolidatedPageID</span>)) <span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> Seperate File ID from Page ID</span>
<span style="color: rgba(0, 0, 255, 1)">SET</span> <span style="color: rgba(0, 128, 0, 1)">@hex_pageid</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">0x</span><span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(128, 128, 128, 1)">+</span> <span style="color: rgba(255, 0, 255, 1)">SUBSTRING</span>(<span style="color: rgba(0, 128, 0, 1)">@ConsolidatedPageID</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(255, 0, 255, 1)">CHARINDEX</span>(<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">:</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">@ConsolidatedPageID</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(128, 128, 128, 1)">+</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, <span style="color: rgba(255, 0, 255, 1)">LEN</span>(<span style="color: rgba(0, 128, 0, 1)">@ConsolidatedPageID</span>)) <span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">-Seperate the page ID</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(0, 128, 0, 1)">@Pageid</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 255, 1)">CONVERT</span>(<span style="color: rgba(0, 0, 255, 1)">INT</span>, <span style="color: rgba(255, 0, 255, 1)">CAST</span>(<span style="color: rgba(255, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">AS</span> XML).value(<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">xs:hexBinary(substring(sql:variable("@hex_pageid"),sql:column("t.pos")) )</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">varbinary(max)</span><span style="color: rgba(255, 0, 0, 1)">'</span>)) <span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> Convert Page ID from hex to integer</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span> ( <span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(255, 0, 255, 1)">CASE</span> <span style="color: rgba(255, 0, 255, 1)">SUBSTRING</span>(<span style="color: rgba(0, 128, 0, 1)">@hex_pageid</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">WHEN</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">0x</span><span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(0, 0, 255, 1)">THEN</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span>
<span style="color: rgba(0, 0, 255, 1)">ELSE</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span>
<span style="color: rgba(0, 0, 255, 1)">END</span><span style="color: rgba(0, 0, 0, 1)">
) </span><span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> t ( pos )
</span><span style="color: rgba(0, 0, 255, 1)">IF</span> <span style="color: rgba(0, 128, 0, 1)">@Context</span> <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">LCX_PFS</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">BEGIN</span>
<span style="color: rgba(0, 0, 255, 1)">DELETE</span> <span style="color: rgba(0, 128, 0, 1)">@temppagedata</span>
<span style="color: rgba(0, 0, 255, 1)">INSERT</span> <span style="color: rgba(0, 0, 255, 1)">INTO</span> <span style="color: rgba(0, 128, 0, 1)">@temppagedata</span>
<span style="color: rgba(0, 0, 255, 1)">EXEC</span><span style="color: rgba(0, 0, 0, 1)">
( </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color


2万+

被折叠的 条评论
为什么被折叠?



