Let me create few objects and drop it to demonstrate the solution.
--This script is compatible with SQL Server 2005 and above.USE tempdbGO--Create SchemaCREATE SCHEMA HumanResourcesGO--Create TableCREATE TABLE HumanResources.[tbl_Employee]( [Sno] [int] NOT NULL, [ID] nvarchar(6) Not NULL , [name] [varchar](50) NOT NULL, [Date of Birth] datetime not null, [Weight] [int] NULL)GO--Create ViewCreate View HumanResources.[vw_Employee]asSelect * from HumanResources.[tbl_Employee]GO--Create ProcedureCreate Procedure HumanResources.[usp_Employee]@EmployeeID nvarchar(6)asSelect * from HumanResources.[tbl_Employee] Where [ID] =@EmployeeIDGO--Create functionCreate FUNCTION HumanResources.[fn_Employee](@EmployeeID nvarchar(6))RETURNS intASBegin Declare @Weight int Select @Weight = [Weight] from HumanResources.[tbl_Employee] Where ID =@EmployeeID Return @WeightEndGO--Create TriggerCREATE TRIGGER HumanResources.trg_EmployeeON [tbl_Employee]FOR INSERTAS RAISERROR (50009, 16, 10)GO--Drop all objectsDROP TRIGGER HumanResources.trg_EmployeeDROP VIEW HumanResources.[vw_Employee]DROP PROCEDURE HumanResources.[usp_Employee]DROP FUNCTION HumanResources.[Fn_Employee]DROP TABLE HumanResources.[tbl_Employee]GO |
Once you dropped all object, you need to create the given below script to recover who dropped what object at what time.
--This script is compatible with SQL Server 2005 and above.USE tempdbGO--DROP PROCEDURE Recover_Dropped_Objects_Detail_Proc--GOCREATE PROCEDURE Recover_Dropped_Objects_Detail_Proc@Date_From DATETIME='1900/01/01',@Date_To DATETIME ='9999/12/31'AS;WITH CTE AS (Select B.name AS [Schema Name],REPLACE(SUBSTRING(A.[RowLog Contents 0],14,LEN(A.[RowLog Contents 0])),0x00,0x) AS [Object Name],[Transaction ID],A.[RowLog Contents 0]FROM fn_dblog(NULL,NULL) ALEFT JOIN sys.schemas BON CONVERT(INT,SUBSTRING([RowLog Contents 0],2,2))= B.schema_idWHERE A.[AllocUnitName] ='sys.sysschobjs.nc1'ANDA.[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] LIKE '%DROP%'AND CONVERT(NVARCHAR(11),[Begin Time]) BETWEEN @Date_From AND @Date_To))SELECT[Schema Name],[Object Name],B.[Begin Time] AS [Dropped Date & Time],C.[name] AS [Dropped By User Name]FROM CTE AINNER JOIN fn_dblog(NULL,NULL) BON A.[Transaction ID] =B.[Transaction ID]AND Context IN ('LCX_NULL') AND Operation IN ('LOP_BEGIN_XACT')AND [Transaction Name]LIKE '%DROP%'INNER JOIN sys.sysusers C ON B.[Transaction SID]=C.[Sid]GO |
No comments:
Post a Comment