----创建存储过程

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.

sqlserver 误删数据恢复的更多相关文章

  1. SqlServer误删数据恢复

    误删数据,操作步骤: 第一步: 找到误删的数据库之前备份文件. 第二步: 1,修改数据库备份模式为:大容量日志 2,修改访问限制为:SINGLE_USER(单用户模式) 第三步: 执行sql一条一条执 ...

  2. 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件

    通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二).   前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...

  3. SQLSERVER误删除了Windows登录用户验证方式使用Windows身份验证的解决方法

    SQLSERVER误删Windows登录用户验证方式使用Windows身份验证的解决方法 今天看到这篇文章:没有了SA密码,无法Windows集成身份登录,DBA怎么办? 想起来之前着急哥问我的一个问 ...

  4. SQLSERVER误删Windows登录用户

    SQLSERVER误删除了Windows登录用户验证方式使用Windows身份验证的解决方法   SQLSERVER误删Windows登录用户验证方式使用Windows身份验证的解决方法 今天看到这篇 ...

  5. sql server误删数据恢复delete(低效版)

    关键词:sql server误删数据恢复,mssql误删数据恢复,delete --切换数据库 use master --构建函数 Create PROCEDURE Recover_Deleted_D ...

  6. SqlServer无备份下误删数据恢复

    系统已上线,给客户修改bug的时候,使用delete语句删表数据,没想到库没切换成测试库.误删了正式库的数据,而且一次备份都没有做过,玩大了 不扯了,进入主题 网上很多方法,都是针对至少有一次备份的情 ...

  7. SQLSERVER2012误删数据恢复过程

    由于长时间从事企业应用系统开发,前往用户现场升级.调试系统是比较常做的事情,但是就在周一,由于同事的失误在毫无知觉的情况下误删了生产数据库几乎所有的数据.当我发现的那一刻,感觉头发都立起来了,心想这他 ...

  8. sqlserver 误删数据库恢复

    本文为转载 原文:https://blog.csdn.net/xwnxwn/article/details/53537841 由于长时间从事企业应用系统开发,前往用户现场升级.调试系统是比较常做的事情 ...

  9. oracle 误删数据恢复

    1.根据时间点查系统版本号scn: select timestamp_to_scn(to_timestamp('2013-01-07 11:20:00','YYYY-MM-DD HH:MI:SS')) ...

随机推荐

  1. Contest 20140708 testB dp 组合数

    testB 输入文件: testB.in  输出文件testB.out 时限3000ms 问题描述: 定义这样一个序列(a1,b1),(a2,b2),…,(ak,bk)如果这个序列是方序列的话必须满足 ...

  2. uva 10130 SuperSale

    一个01背包问题: 刚刚开始把题目看错了,以为物品的数目是有限的,然后让你求一个家庭里最多能够拿多个价值的东西: 这样一来的话,这个题目就有点意思了: 但是后来发现竟然是个简单的01背包问题 =  = ...

  3. 【POJ3208】 (DP)

    Apocalypse Someday Description The number 666 is considered to be the occult “number of the beast” a ...

  4. FLASH 存储学习-串行SPI NOR FLASH

    1.1 SST25VF080B简介1.1.1 主要特性 关键点:容量.速度(时钟速度.读写速度).功耗. l 容量:8MBit: l 最高SPI时钟频率:50MHz: l 低功耗模式下电流消耗:5uA ...

  5. 一些记录查询的SQL语句

    -- ======================== 第三天 =========================== CREATE DATABASE php0408 CHARSET utf8 ;CR ...

  6. 2.5.2 使用alertdialog 创建列表对话框

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  7. CImg 读取jpg, png ,tif 等格式失败解决方案

    CImg homepage :http://cimg.sourceforge.net CImg 给出的一个简单的示例:http://cimg.sourceforge.net/reference/gro ...

  8. [QT]Qt+VS2012+Win8 64Bit安装

    学习Qt鸟,当年没听@Coding_Peon(http://weibo.com/u/1764451551?topnav=1&wvr=5&topsug=1)话好好学习QT和Python之 ...

  9. Bzoj 2763: [JLOI2011]飞行路线 拆点,分层图,最短路,SPFA

    2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1694  Solved: 635[Submit][Statu ...

  10. int 和 long的区别

    数据模型决定了C语言中基本数据类型与数据宽度的对应关系.我们常用的32位模型称为ILP32,而64位模型有三种:LP64.LLP64和ILP64.在64位模型中,指针一定是64位的,但是int和lon ...