sqlserver 误删数据恢复

----创建存储过程

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()
DECLARE @TransactionID NVARCHAR(MAX)
DECLARE @AllocUnitID BIGINT
DECLARE @AllocUnitName NVARCHAR(MAX)
DECLARE @SQL NVARCHAR(MAX)
DECLARE @Compatibility_Level INT SELECT @Compatibility_Level = dtb.compatibility_level
FROM master.sys.databases AS dtb
WHERE dtb.name = @Database_Name IF ISNULL(@Compatibility_Level, ) <=
BEGIN
RAISERROR('The compatibility level should be equal to or greater SQL SERVER 2005 (90)',,)
RETURN
END IF ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE [TABLE_SCHEMA] + '.' + [TABLE_NAME] = @SchemaName_n_TableName
) =
BEGIN
RAISERROR('Could not found the table in the defined database',,)
RETURN
END DECLARE @bitTable TABLE
(
[ID] INT ,
[Bitvalue] INT
)
--Create table to set the bit position of one byte. INSERT INTO @bitTable
SELECT , UNION ALL
SELECT , UNION ALL
SELECT , UNION ALL
SELECT , UNION ALL
SELECT , UNION ALL
SELECT , UNION ALL
SELECT , UNION ALL
SELECT , --Create table to collect the row data.
DECLARE @DeletedRecords TABLE
(
[Row ID] INT IDENTITY(, ) ,
[RowLogContents] VARBINARY() ,
[AllocUnitID] BIGINT ,
[Transaction ID] NVARCHAR(MAX) ,
[FixedLengthData] SMALLINT ,
[TotalNoOfCols] SMALLINT ,
[NullBitMapLength] SMALLINT ,
[NullBytes] VARBINARY() ,
[TotalNoofVarCols] SMALLINT ,
[ColumnOffsetArray] VARBINARY() ,
[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 ] AS [RowLogContents] ,
[AllocUnitID] AS [AllocUnitID] ,
[Transaction ID] AS [Transaction ID] --[Fixed Length Data] = Substring (RowLog content , Status Bit A+ Status Bit B + , bytes)
,
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) AS [FixedLengthData] --@FixedLengthData -- [TotalnoOfCols] = Substring (RowLog content , [Fixed Length Data] + , bytes)
,
CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) + ,
)))) AS [TotalNoOfCols] --[NullBitMapLength]=ceiling([Total No of Columns] /8.0)
,
CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) + ,
)))) / 8.0)) AS [NullBitMapLength] --[Null Bytes] = Substring (RowLog content , Status Bit A+ Status Bit B + [Fixed Length Data] +, [NullBitMapLength] )
,
SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) + ,
CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) + ,
)))) / 8.0))) AS [NullBytes] --[TotalNoofVarCols] = Substring (RowLog content , Status Bit A+ Status Bit B + [Fixed Length Data] +, [Null Bitmap length] + )
,
( CASE WHEN SUBSTRING([RowLog Contents ], , ) IN (
0x10, 0x30, 0x70 )
THEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) +
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) + ,
)))) / 8.0)), ))))
ELSE NULL
END ) AS [TotalNoofVarCols] --[ColumnOffsetArray]= Substring (RowLog content , Status Bit A+ Status Bit B + [Fixed Length Data] +, [Null Bitmap length] + , [TotalNoofVarCols]* )
,
( CASE WHEN SUBSTRING([RowLog Contents ], , ) IN (
0x10, 0x30, 0x70 )
THEN SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) +
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) + ,
)))) / 8.0))
+ ,
( CASE WHEN SUBSTRING([RowLog Contents ],
, ) IN ( 0x10,
0x30, 0x70 )
THEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) +
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) + ,
)))) / 8.0)), ))))
ELSE NULL
END ) * )
ELSE NULL
END ) AS [ColumnOffsetArray] -- Variable column Start = Status Bit A+ Status Bit B + [Fixed Length Data] + [Null Bitmap length] + +([TotalNoofVarCols]*)
,
CASE WHEN SUBSTRING([RowLog Contents ], , ) IN (
0x10, 0x30, 0x70 )
THEN ( CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) +
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) + ,
)))) / 8.0))
+ ( ( CASE WHEN SUBSTRING([RowLog Contents ],
, ) IN ( 0x10,
0x30, 0x70 )
THEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) +
+ CONVERT(INT, CEILING(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(SUBSTRING([RowLog Contents ],
+ , )))) + ,
)))) / 8.0)), ))))
ELSE NULL
END ) * ) )
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 (
, )
AND partitions.hobt_id = allocunits.container_id
)
OR ( allocunits.type =
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 ], , ) 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(), [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
UNION ALL
SELECT
),
N2 ( n )
AS ( SELECT
FROM N1 AS X ,
N1 AS Y
),
N3 ( n )
AS ( SELECT
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 ( means null zero means not null)
,
[NullBitMap] = ( REPLACE(STUFF(( SELECT
','
+ ( CASE
WHEN [ID] =
THEN CONVERT(NVARCHAR(), ( SUBSTRING(NullBytes,
n, ) % ))
ELSE CONVERT(NVARCHAR(), ( ( SUBSTRING(NullBytes,
n, )
/ [Bitvalue] )
% ))
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('')
), , , ''), ',', '') )
FROM RowData D IF ( SELECT COUNT(*)
FROM @DeletedRecords
) =
BEGIN
RAISERROR('There is no data in the log as per the search criteria',,)
RETURN
END DECLARE @ColumnNameAndData TABLE
(
[Row ID] INT ,
[Rowlogcontents] VARBINARY(MAX) ,
[NAME] SYSNAME ,
[nullbit] SMALLINT ,
[leaf_offset] SMALLINT ,
[length] SMALLINT ,
[system_type_id] TINYINT ,
[bitpos] TINYINT ,
[xprec] TINYINT ,
[xscale] TINYINT ,
[is_null] INT ,
[Column value Size] INT ,
[Column Length] INT ,
[hex_Value] VARBINARY(MAX) ,
[Slot ID] INT ,
[Update] INT
) --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) + ,
--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, ) AS is_null ,
( CASE WHEN leaf_offset <
AND SUBSTRING([nullBitMap], cols.leaf_null_bit,
) =
THEN ( CASE WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) >
THEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
- POWER(, )
ELSE CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
END )
END ) AS [Column value Size] ,
( CASE WHEN leaf_offset <
AND SUBSTRING([nullBitMap], cols.leaf_null_bit,
) =
THEN ( CASE WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) >
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) <
THEN ( CASE WHEN [System_type_id] IN (
, , ) THEN
ELSE
END )
WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) >
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) >
THEN ( CASE WHEN [System_type_id] IN (
, , ) THEN
ELSE
END ) --
WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) <
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) <
THEN ( CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) )
WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) <
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) >
THEN POWER(, )
+ CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart])
END )
END ) AS [Column Length] ,
( CASE WHEN SUBSTRING([nullBitMap], cols.leaf_null_bit, ) =
THEN NULL
ELSE SUBSTRING(Rowlogcontents,
( ( CASE WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) >
THEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
- POWER(, )
ELSE CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
END )
- ( CASE WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) >
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) <
THEN ( CASE
WHEN [System_type_id] IN (
, , )
THEN
ELSE
END ) --
WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) >
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) >
THEN ( CASE
WHEN [System_type_id] IN (
, , )
THEN
ELSE
END ) --
WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) <
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) <
THEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart])
WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) <
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) >
THEN POWER(, )
+ CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart])
END ) ) + ,
( CASE WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) >
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) <
THEN ( CASE WHEN [System_type_id] IN (
, , )
THEN
ELSE
END ) --
WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) >
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) >
THEN ( CASE WHEN [System_type_id] IN (
, , )
THEN
ELSE
END ) --
WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) <
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) <
THEN ABS(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]))
WHEN CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , )))) <
AND ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart]) >
THEN POWER(, )
+ CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* leaf_offset
* - ) - , ))))
- ISNULL(NULLIF(CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING([ColumnOffsetArray],
(
* ( ( leaf_offset
* - ) - ) )
- , )))), ),
[varColumnStart])
END ))
END ) AS hex_Value ,
[Slot ID] , 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 (
, )
AND partitions.hobt_id = allocunits.container_id
)
OR ( allocunits.type =
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 <
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, ) AS is_null ,
( SELECT TOP
ISNULL(SUM(CASE WHEN C.leaf_offset >
THEN max_length
ELSE
END), )
FROM sys.system_internals_partition_columns C
WHERE cols.partition_id = C.partition_id
AND C.leaf_null_bit < cols.leaf_null_bit
) + AS [Column value Size] ,
syscolumns.length AS [Column Length] ,
CASE WHEN SUBSTRING([nullBitMap], cols.leaf_null_bit, ) =
THEN NULL
ELSE SUBSTRING(Rowlogcontents,
( SELECT TOP
ISNULL(SUM(CASE
WHEN C.leaf_offset >
AND C.leaf_bit_position =
THEN max_length
ELSE
END), )
FROM sys.system_internals_partition_columns C
WHERE cols.partition_id = C.partition_id
AND C.leaf_null_bit < cols.leaf_null_bit
) + , syscolumns.length)
END AS hex_Value ,
[Slot ID] , 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 (
, )
AND partitions.hobt_id = allocunits.container_id
)
OR ( allocunits.type =
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 >
ORDER BY nullbit DECLARE @BitColumnByte AS INT
SELECT @BitColumnByte = CONVERT(INT, CEILING(COUNT(*) / 8.0))
FROM @ColumnNameAndData
WHERE [System_Type_id] = ;
WITH N1 ( n )
AS ( SELECT
UNION ALL
SELECT
),
N2 ( n )
AS ( SELECT
FROM N1 AS X ,
N1 AS Y
),
N3 ( n )
AS ( SELECT
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
),
CTE
AS ( SELECT RowLogContents ,
[nullbit] ,
[BitMap] = CONVERT(VARBINARY(), CONVERT(INT, SUBSTRING(( REPLACE(STUFF(( SELECT
','
+ ( CASE
WHEN [ID] =
THEN CONVERT(NVARCHAR(), ( SUBSTRING(hex_Value,
n, ) % ))
ELSE CONVERT(NVARCHAR(), ( ( SUBSTRING(hex_Value,
n, )
/ [Bitvalue] )
% ))
END ) --as [nullBitMap]
FROM
N4 AS Nums
JOIN @ColumnNameAndData
AS C ON n <= @BitColumnByte
AND [System_Type_id] =
AND bitpos =
CROSS JOIN @bitTable
WHERE
C.[RowLogContents] = D.[RowLogContents]
ORDER BY [RowLogContents] ,
n ASC
FOR
XML
PATH('')
), , , ''),
',', '') ),
bitpos + , )))
FROM @ColumnNameAndData D
WHERE [System_Type_id] =
)
UPDATE A
SET [hex_Value] = [BitMap]
FROM @ColumnNameAndData A
INNER JOIN CTE B ON A.[RowLogContents] = B.[RowLogContents]
AND A.[nullbit] = B.[nullbit] /**************Check for BLOB DATA TYPES******************************/
DECLARE @Fileid INT
DECLARE @Pageid INT
DECLARE @Slotid INT
DECLARE @CurrentLSN INT
DECLARE @LinkID INT
DECLARE @Context VARCHAR()
DECLARE @ConsolidatedPageID VARCHAR(MAX)
DECLARE @LCX_TEXT_MIX VARBINARY(MAX) DECLARE @temppagedata TABLE
(
[ParentObject] SYSNAME ,
[Object] SYSNAME ,
[Field] SYSNAME ,
[Value] SYSNAME
) DECLARE @pagedata TABLE
(
[Page ID] SYSNAME ,
[File IDS] INT ,
[Page IDS] INT ,
[AllocUnitId] BIGINT ,
[ParentObject] SYSNAME ,
[Object] SYSNAME ,
[Field] SYSNAME ,
[Value] SYSNAME
) DECLARE @ModifiedRawData TABLE
(
[ID] INT IDENTITY(, ) ,
[PAGE ID] VARCHAR(MAX) ,
[FILE IDS] INT ,
[PAGE IDS] INT ,
[Slot ID] INT ,
[AllocUnitId] BIGINT ,
[RowLog Contents 0_var] VARCHAR(MAX) ,
[RowLog Length] VARCHAR() ,
[RowLog Len] INT ,
[RowLog Contents ] VARBINARY(MAX) ,
[Link ID] INT DEFAULT ( ) ,
[Update] INT
) DECLARE Page_Data_Cursor CURSOR
FOR
/*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*/
SELECT LTRIM(RTRIM(REPLACE([Description], 'Deallocated', ''))) AS [PAGE ID] ,
[Slot ID] ,
[AllocUnitId] ,
NULL AS [RowLog Contents ] ,
NULL AS [RowLog Contents ] ,
Context
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 (
, )
AND partitions.hobt_id = allocunits.container_id
)
OR ( allocunits.type =
AND partitions.partition_id = allocunits.container_id
)
WHERE object_id = OBJECT_ID('' + @SchemaName_n_TableName
+ '') )
AND Operation IN ( 'LOP_MODIFY_ROW' )
AND [Context] IN ( 'LCX_PFS' )
AND Description LIKE '%Deallocated%'
/*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] = 'DELETE'
AND CONVERT(NVARCHAR(), [Begin Time]) BETWEEN @Date_From
AND
@Date_To )
GROUP BY [Description] ,
[Slot ID] ,
[AllocUnitId] ,
Context
UNION
SELECT [PAGE ID] ,
[Slot ID] ,
[AllocUnitId] ,
SUBSTRING([RowLog Contents ], ,
LEN([RowLog Contents ])) AS [RowLog Contents ] ,
CONVERT(INT, SUBSTRING([RowLog Contents ], , )) ,
Context --,CAST(RIGHT([Current LSN],) AS INT) AS [Current LSN]
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 (
, )
AND partitions.hobt_id = allocunits.container_id
)
OR ( allocunits.type =
AND partitions.partition_id = allocunits.container_id
)
WHERE object_id = OBJECT_ID('' + @SchemaName_n_TableName
+ '') )
AND Context IN ( 'LCX_TEXT_MIX' )
AND Operation IN ( 'LOP_DELETE_ROWS' )
/*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] = 'DELETE'
AND CONVERT(NVARCHAR(), [Begin Time]) BETWEEN @Date_From
AND
@Date_To ) /****************************************/ OPEN Page_Data_Cursor FETCH NEXT FROM Page_Data_Cursor INTO @ConsolidatedPageID, @Slotid,
@AllocUnitID, @LCX_TEXT_MIX, @LinkID, @Context WHILE @@FETCH_STATUS =
BEGIN
DECLARE @hex_pageid AS VARCHAR(MAX)
/*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*/
SET @Fileid = SUBSTRING(@ConsolidatedPageID, ,
CHARINDEX(':', @ConsolidatedPageID)) -- Seperate File ID from Page ID SET @hex_pageid = '0x' + SUBSTRING(@ConsolidatedPageID,
CHARINDEX(':',
@ConsolidatedPageID)
+ , LEN(@ConsolidatedPageID)) ---Seperate the page ID
SELECT @Pageid = CONVERT(INT, CAST('' AS XML).value('xs:hexBinary(substring(sql:variable("@hex_pageid"),sql:column("t.pos")) )',
'varbinary(max)')) -- Convert Page ID from hex to integer
FROM ( SELECT CASE SUBSTRING(@hex_pageid, , )
WHEN '0x' THEN
ELSE
END
) AS t ( pos ) IF @Context = 'LCX_PFS'
BEGIN
DELETE @temppagedata
INSERT INTO @temppagedata
EXEC
( 'DBCC PAGE(' + @DataBase_Name + ', '
+ @fileid + ', ' + @pageid
+ ', 1) with tableresults,no_infomsgs;'
);
INSERT INTO @pagedata
SELECT @ConsolidatedPageID ,
@fileid ,
@pageid ,
@AllocUnitID ,
[ParentObject] ,
[Object] ,
[Field] ,
[Value]
FROM @temppagedata
END
ELSE
IF @Context = 'LCX_TEXT_MIX'
BEGIN
INSERT INTO @ModifiedRawData
SELECT @ConsolidatedPageID ,
@fileid ,
@pageid ,
@Slotid ,
@AllocUnitID ,
NULL ,
,
CONVERT(INT, CONVERT(VARBINARY, REVERSE(SUBSTRING(@LCX_TEXT_MIX,
, )))) ,
@LCX_TEXT_MIX ,
@LinkID , END
FETCH NEXT FROM Page_Data_Cursor INTO @ConsolidatedPageID, @Slotid,
@AllocUnitID, @LCX_TEXT_MIX, @LinkID, @Context
END CLOSE Page_Data_Cursor
DEALLOCATE Page_Data_Cursor DECLARE @Newhexstring VARCHAR(MAX); --The data is in multiple rows in the page, so we need to convert it into one row as a single hex value.
--This hex value is in string format
INSERT INTO @ModifiedRawData
( [PAGE ID] ,
[FILE IDS] ,
[PAGE IDS] ,
[Slot ID] ,
[AllocUnitId] ,
[RowLog Contents 0_var] ,
[RowLog Length]
)
SELECT [Page ID] ,
[FILE IDS] ,
[PAGE IDS] ,
SUBSTRING([ParentObject],
CHARINDEX('Slot', [ParentObject]) + ,
( CHARINDEX('Offset', [ParentObject])
- ( CHARINDEX('Slot', [ParentObject]) + ) )
- ) AS [Slot ID] ,
[AllocUnitId] ,
SUBSTRING(( SELECT REPLACE(STUFF(( SELECT
REPLACE(SUBSTRING([Value],
CHARINDEX(':',
[Value]) + ,
CHARINDEX('†',
[Value])
- CHARINDEX(':',
[Value])), '†',
'')
FROM @pagedata C
WHERE B.[Page ID] = C.[Page ID]
AND SUBSTRING(B.[ParentObject],
CHARINDEX('Slot',
B.[ParentObject])
+ ,
( CHARINDEX('Offset',
B.[ParentObject])
- ( CHARINDEX('Slot',
B.[ParentObject])
+ ) )) = SUBSTRING(C.[ParentObject],
CHARINDEX('Slot',
C.[ParentObject])
+ ,
( CHARINDEX('Offset',
C.[ParentObject])
- ( CHARINDEX('Slot',
C.[ParentObject])
+ ) ))
AND [Object] LIKE '%Memory Dump%'
ORDER BY '0x'
+ LEFT([Value],
CHARINDEX(':',
[Value]) - )
FOR
XML PATH('')
), , , ''), ' ', '')
), , ) AS [Value] ,
SUBSTRING(( SELECT '0x'
+ REPLACE(STUFF(( SELECT
REPLACE(SUBSTRING([Value],
CHARINDEX(':',
[Value]) + ,
CHARINDEX('†',
[Value])
- CHARINDEX(':',
[Value])), '†',
'')
FROM
@pagedata C
WHERE
B.[Page ID] = C.[Page ID]
AND SUBSTRING(B.[ParentObject],
CHARINDEX('Slot',
B.[ParentObject])
+ ,
( CHARINDEX('Offset',
B.[ParentObject])
- ( CHARINDEX('Slot',
B.[ParentObject])
+ ) )) = SUBSTRING(C.[ParentObject],
CHARINDEX('Slot',
C.[ParentObject])
+ ,
( CHARINDEX('Offset',
C.[ParentObject])
- ( CHARINDEX('Slot',
C.[ParentObject])
+ ) ))
AND [Object] LIKE '%Memory Dump%'
ORDER BY '0x'
+ LEFT([Value],
CHARINDEX(':',
[Value]) - )
FOR
XML PATH('')
), , , ''), ' ', '')
), , ) AS [Length]
FROM @pagedata B
WHERE [Object] LIKE '%Memory Dump%'
GROUP BY [Page ID] ,
[FILE IDS] ,
[PAGE IDS] ,
[ParentObject] ,
[AllocUnitId]--,[Current LSN]
ORDER BY [Slot ID] UPDATE @ModifiedRawData
SET [RowLog Len] = CONVERT(VARBINARY(), REVERSE(CAST('' AS XML).value('xs:hexBinary(substring(sql:column("[RowLog Length]"),0))',
'varbinary(Max)')))
FROM @ModifiedRawData
WHERE [LINK ID] = UPDATE @ModifiedRawData
SET [RowLog Contents ] = CAST('' AS XML).value('xs:hexBinary(substring(sql:column("[RowLog Contents 0_var]"),0))',
'varbinary(Max)')
FROM @ModifiedRawData
WHERE [LINK ID] = UPDATE B
SET B.[RowLog Contents ] = ( CASE WHEN A.[RowLog Contents ] IS NOT NULL
AND C.[RowLog Contents ] IS NOT NULL
THEN A.[RowLog Contents ]
+ C.[RowLog Contents ]
WHEN A.[RowLog Contents ] IS NULL
AND C.[RowLog Contents ] IS NOT NULL
THEN C.[RowLog Contents ]
WHEN A.[RowLog Contents ] IS NOT NULL
AND C.[RowLog Contents ] IS NULL
THEN A.[RowLog Contents ]
END ) ,
B.[Update] = ISNULL(B.[Update], ) +
FROM @ModifiedRawData B
LEFT JOIN @ModifiedRawData A ON A.[Page IDS] = CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING(B.[RowLog Contents ],
+ , ))))
AND A.[File IDS] = CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING(B.[RowLog Contents ],
+ , ))))
AND A.[Link ID] = B.[Link ID]
LEFT JOIN @ModifiedRawData C ON C.[Page IDS] = CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING(B.[RowLog Contents ],
+ , ))))
AND C.[File IDS] = CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING(B.[RowLog Contents ],
+ , ))))
AND C.[Link ID] = B.[Link ID]
WHERE ( A.[RowLog Contents ] IS NOT NULL
OR C.[RowLog Contents ] IS NOT NULL
) UPDATE B
SET B.[RowLog Contents ] = ( CASE WHEN A.[RowLog Contents ] IS NOT NULL
AND C.[RowLog Contents ] IS NOT NULL
THEN A.[RowLog Contents ]
+ C.[RowLog Contents ]
WHEN A.[RowLog Contents ] IS NULL
AND C.[RowLog Contents ] IS NOT NULL
THEN C.[RowLog Contents ]
WHEN A.[RowLog Contents ] IS NOT NULL
AND C.[RowLog Contents ] IS NULL
THEN A.[RowLog Contents ]
END )
--,B.[Update]=ISNULL(B.[Update],)+
FROM @ModifiedRawData B
LEFT JOIN @ModifiedRawData A ON A.[Page IDS] = CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING(B.[RowLog Contents ],
+ , ))))
AND A.[File IDS] = CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING(B.[RowLog Contents ],
+ , ))))
AND A.[Link ID] <> B.[Link ID]
AND B.[Update] =
LEFT JOIN @ModifiedRawData C ON C.[Page IDS] = CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING(B.[RowLog Contents ],
+ , ))))
AND C.[File IDS] = CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING(B.[RowLog Contents ],
+ , ))))
AND C.[Link ID] <> B.[Link ID]
AND B.[Update] =
WHERE ( A.[RowLog Contents ] IS NOT NULL
OR C.[RowLog Contents ] IS NOT NULL
) UPDATE @ModifiedRawData
SET [RowLog Contents ] = ( CASE WHEN [RowLog Len] >=
THEN SUBSTRING([RowLog Contents ],
, [RowLog Len])
WHEN [RowLog Len] <
THEN SUBSTRING([RowLog Contents ],
+ ,
CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING([RowLog Contents ],
, )))))
END )
FROM @ModifiedRawData
WHERE [LINK ID] = UPDATE @ColumnNameAndData
SET [hex_Value] = [RowLog Contents ]
--,A.[Update]=A.[Update]+
FROM @ColumnNameAndData A
INNER JOIN @ModifiedRawData B ON CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING([hex_value],
, )))) = [PAGE IDS]
AND CONVERT(INT, SUBSTRING([hex_value],
, )) = B.[Link ID]
WHERE [System_Type_Id] IN ( , , , , , , , )
AND [Link ID] <> UPDATE @ColumnNameAndData
SET [hex_Value] = ( CASE WHEN B.[RowLog Contents ] IS NOT NULL
AND C.[RowLog Contents ] IS NOT NULL
THEN B.[RowLog Contents ]
+ C.[RowLog Contents ]
WHEN B.[RowLog Contents ] IS NULL
AND C.[RowLog Contents ] IS NOT NULL
THEN C.[RowLog Contents ]
WHEN B.[RowLog Contents ] IS NOT NULL
AND C.[RowLog Contents ] IS NULL
THEN B.[RowLog Contents ]
END )
--,A.[Update]=A.[Update]+
FROM @ColumnNameAndData A
LEFT JOIN @ModifiedRawData B ON CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING([hex_value],
, )))) = B.[PAGE IDS]
AND B.[Link ID] =
LEFT JOIN @ModifiedRawData C ON CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING([hex_value],
, )))) = C.[PAGE IDS]
AND C.[Link ID] =
WHERE [System_Type_Id] IN ( , , , , , , , )
AND ( B.[RowLog Contents ] IS NOT NULL
OR C.[RowLog Contents ] IS NOT NULL
) UPDATE @ColumnNameAndData
SET [hex_Value] = [RowLog Contents ]
--,A.[Update]=A.[Update]+
FROM @ColumnNameAndData A
INNER JOIN @ModifiedRawData B ON CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING([hex_value],
, )))) = [PAGE IDS]
AND CONVERT(INT, SUBSTRING([hex_value],
, )) = [Link ID]
WHERE [System_Type_Id] IN ( , , )
AND [Link ID] <> UPDATE @ColumnNameAndData
SET [hex_Value] = [RowLog Contents ]
--,A.[Update]=A.[Update]+
FROM @ColumnNameAndData A
INNER JOIN @ModifiedRawData B ON CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING([hex_value],
, )))) = [PAGE IDS]
WHERE [System_Type_Id] IN ( , , )
AND [Link ID] = UPDATE @ColumnNameAndData
SET [hex_Value] = [RowLog Contents ]
--,A.[Update]=A.[Update]+
FROM @ColumnNameAndData A
INNER JOIN @ModifiedRawData B ON CONVERT(INT, CONVERT(VARBINARY(MAX), REVERSE(SUBSTRING([hex_value],
, )))) = [PAGE IDS]
WHERE [System_Type_Id] IN ( , , )
AND [Link ID] = UPDATE @ColumnNameAndData
SET [hex_value] = 0xFFFE + SUBSTRING([hex_value], , LEN([hex_value]))
--,[Update]=[Update]+
WHERE [system_type_id] = CREATE TABLE [#temp_Data]
(
[FieldName] VARCHAR(MAX) ,
[FieldValue] NVARCHAR(MAX) ,
[Rowlogcontents] VARBINARY() ,
[Row ID] INT
) INSERT INTO #temp_Data
SELECT NAME ,
CASE WHEN system_type_id IN ( , )
THEN LTRIM(RTRIM(CONVERT(NVARCHAR(MAX), hex_Value))) --NVARCHAR ,NCHAR
WHEN system_type_id IN ( , )
THEN LTRIM(RTRIM(CONVERT(VARCHAR(MAX), hex_Value))) --VARCHAR,CHAR
WHEN system_type_id IN ( )
THEN LTRIM(RTRIM(CONVERT(VARCHAR(MAX), hex_Value))) --Text
WHEN system_type_id IN ( )
THEN LTRIM(RTRIM(CONVERT(NVARCHAR(MAX), hex_Value))) --nText
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(TINYINT, CONVERT(BINARY(), REVERSE(hex_Value)))) --TINY INTEGER
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(SMALLINT, CONVERT(BINARY(), REVERSE(hex_Value)))) --SMALL INTEGER
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(INT, CONVERT(BINARY(), REVERSE(hex_Value)))) -- INTEGER
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(BIGINT, CONVERT(BINARY(), REVERSE(hex_Value))))-- BIG INTEGER
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(DATETIME, CONVERT(VARBINARY(), REVERSE(hex_Value))), ) --DATETIME
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(SMALLDATETIME, CONVERT(VARBINARY(), REVERSE(hex_Value))), ) --SMALL DATETIME
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(NUMERIC(, ), CONVERT(VARBINARY, CONVERT(VARBINARY(), xprec)
+ CONVERT(VARBINARY(), xscale))
+ CONVERT(VARBINARY(), ) + hex_Value)) --- NUMERIC
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(DECIMAL(, ), CONVERT(VARBINARY, CONVERT(VARBINARY(), xprec)
+ CONVERT(VARBINARY(), xscale))
+ CONVERT(VARBINARY(), ) + hex_Value)) --- DECIMAL
WHEN system_type_id IN ( , )
THEN CONVERT(VARCHAR(MAX), CONVERT(MONEY, CONVERT(VARBINARY(), REVERSE(hex_Value))), ) --MONEY,SMALLMONEY
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT (BIT, CONVERT(BINARY(), hex_Value)
% )) -- BIT
WHEN system_type_id =
THEN RTRIM(LTRIM(STR(CONVERT(FLOAT, SIGN(CAST(CONVERT(VARBINARY(), REVERSE(hex_Value)) AS BIGINT))
* ( 1.0
+ ( CAST(CONVERT(VARBINARY(), REVERSE(hex_Value)) AS BIGINT)
& 0x000FFFFFFFFFFFFF )
* POWER(CAST( AS FLOAT),
-) )
* POWER(CAST( AS FLOAT),
( ( CAST(CONVERT(VARBINARY(), REVERSE(hex_Value)) AS BIGINT)
& 0x7ff0000000000000 )
/ EXP( * LOG())
- ))), ,
LEN(hex_Value)))) --- FLOAT
WHEN system_type_id =
THEN LEFT(LTRIM(STR(CAST(SIGN(CAST(CONVERT(VARBINARY(), REVERSE(hex_Value)) AS BIGINT))
* ( 1.0
+ ( CAST(CONVERT(VARBINARY(), REVERSE(hex_Value)) AS BIGINT)
& 0x007FFFFF )
* POWER(CAST( AS REAL), -) )
* POWER(CAST( AS REAL),
( ( ( CAST(CONVERT(VARBINARY(), REVERSE(hex_Value)) AS INT) )
& 0x7f800000 )
/ EXP( * LOG())
- )) AS REAL), ,
)), ) --Real
WHEN system_type_id IN ( , )
THEN ( CASE WHEN CHARINDEX(0x,
CAST('' AS XML).value('xs:hexBinary(sql:column("hex_Value"))',
'VARBINARY(8000)')) =
THEN '0x'
ELSE ''
END ) + CAST('' AS XML).value('xs:hexBinary(sql:column("hex_Value"))',
'varchar(max)') -- BINARY,VARBINARY
WHEN system_type_id =
THEN ( CASE WHEN CHARINDEX(0x,
CAST('' AS XML).value('xs:hexBinary(sql:column("hex_Value"))',
'VARBINARY(8000)')) =
THEN '0x'
ELSE ''
END ) + CAST('' AS XML).value('xs:hexBinary(sql:column("hex_Value"))',
'varchar(max)') --IMAGE
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(UNIQUEIDENTIFIER, hex_Value)) --UNIQUEIDENTIFIER
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(SYSNAME, hex_Value)) --SYSNAME
WHEN system_type_id =
THEN CONVERT(VARCHAR(MAX), CONVERT(XML, hex_Value)) --XML
WHEN system_type_id =
THEN ( CASE WHEN CHARINDEX(0x,
CAST('' AS XML).value('xs:hexBinary(sql:column("hex_Value"))',
'VARBINARY(8000)')) =
THEN '0x'
ELSE ''
END ) + CAST('' AS XML).value('xs:hexBinary(sql:column("hex_Value"))',
'varchar(max)') --TIMESTAMP
WHEN system_type_id =
THEN ( CASE WHEN CONVERT(INT, SUBSTRING(hex_Value, ,
)) =
THEN CONVERT(VARCHAR(MAX), CONVERT(INT, CONVERT(BINARY(), REVERSE(SUBSTRING(hex_Value,
,
LEN(hex_Value)))))) -- INTEGER
WHEN CONVERT(INT, SUBSTRING(hex_Value, ,
)) =
THEN CONVERT(VARCHAR(MAX), CONVERT(NUMERIC(,
), CONVERT(VARBINARY(), SUBSTRING(hex_Value,
, ))
+ CONVERT(VARBINARY(), SUBSTRING(hex_Value,
, ))
+ CONVERT(VARBINARY(), )
+ SUBSTRING(hex_Value, ,
LEN(hex_Value)))) --- NUMERIC
WHEN CONVERT(INT, SUBSTRING(hex_Value, ,
)) =
THEN LTRIM(RTRIM(CONVERT(VARCHAR(MAX), SUBSTRING(hex_Value,
,
LEN(hex_Value))))) --VARCHAR,CHAR
WHEN CONVERT(INT, SUBSTRING(hex_Value, ,
)) =
THEN CONVERT(VARCHAR(MAX), CONVERT(UNIQUEIDENTIFIER, SUBSTRING(( hex_Value ),
, ))) --UNIQUEIDENTIFIER
WHEN CONVERT(INT, SUBSTRING(hex_Value, ,
)) =
THEN CONVERT(VARCHAR(MAX), CONVERT(DATETIME, CONVERT(VARBINARY(), REVERSE(SUBSTRING(hex_Value,
,
LEN(hex_Value))))), ) --DATETIME
WHEN CONVERT(INT, SUBSTRING(hex_Value, ,
)) =
THEN '0x'
+ SUBSTRING(( CASE WHEN CHARINDEX(0x,
CAST('' AS XML).value('xs:hexBinary(sql:column("hex_Value"))',
'VARBINARY(8000)')) =
THEN '0x'
ELSE ''
END )
+ CAST('' AS XML).value('xs:hexBinary(sql:column("hex_Value"))',
'varchar(max)'),
, LEN(hex_Value)) -- BINARY,VARBINARY
END )
END AS FieldValue ,
[Rowlogcontents] ,
[Row ID]
FROM @ColumnNameAndData
ORDER BY nullbit --Create the column name in the same order to do pivot table. DECLARE @FieldName VARCHAR(MAX)
SET @FieldName = STUFF(( SELECT ','
+ CAST(QUOTENAME([Name]) AS VARCHAR(MAX))
FROM syscolumns
WHERE id = OBJECT_ID(''
+ @SchemaName_n_TableName
+ '')
FOR
XML PATH('')
), , , '') --Finally did pivot table and get the data back in the same format. SET @sql = 'SELECT ' + @FieldName
+ ' FROM #temp_Data PIVOT (Min([FieldValue]) FOR FieldName IN ('
+ @FieldName + ')) AS pvt'
EXEC sp_executesql @sql GO

---执行存储过程两个参数(数据库名,表名)

EXEC Recover_Deleted_Data_Proc '数据库名称','dbo.tableName'--表名前面要加dbo 不然会报错的。

--执行存储过程四个参数(数据库名称,表明,开始时间,结束时间【按照时间段来恢复】)

EXEC Recover_Deleted_Data_Proc '数据库名称','dbo.TableName','2014-04-23','2014-04-23'--表名要加dbo。

转自http://www.cnblogs.com/lyhabc/p/3683147.html#2924733.

上一篇:notepad++调用python3中文乱码


下一篇:MVC Controller return 格式分类及用法