
/************TABLE*****************/
CREATE TABLE [PE].[WorkingInstruction](
	[WorkingInstructionID] [int] IDENTITY(1,1) NOT NULL,
	[WorkingInstructionNo] [nvarchar](20) NOT NULL,
	[WorkingInstructionName] [nvarchar](30) NOT NULL,
	[IsDeleted] [bit] NOT NULL,
 CONSTRAINT [PK_WorkingInstruction] PRIMARY KEY NONCLUSTERED 
(
	[WorkingInstructionID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [UK_WorkingInstruction] UNIQUE CLUSTERED 
(
	[WorkingInstructionNo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [UK2_WorkingInstruction] UNIQUE NONCLUSTERED 
(
	[WorkingInstructionName] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [PE].[WorkingInstruction] ADD  CONSTRAINT [DF_WorkingInstruction_IsDeleted]  DEFAULT ((0)) FOR [IsDeleted]
GO

/************GENERATED TSQL for TableRecovery UDF by pAutoAudit**********************/

CREATE FUNCTION [App].[ApplicationRole_TableRecovery]
			(
			@RecoveryTime datetime
			)

 -- generated by AutoAudit Version 3.30 on Dec 13 2013  8:51AM
 -- created by Paul Nielsen and John Sigouin 
 -- www.SQLServerBible.com 
 -- AutoAudit.codeplex.com 
 -- This function retrieves an image of the table as it existed at the specified point in time.

RETURNS @HistoryData Table
(
[RoleID] int
,[RoleName] nvarchar (60)
,[RoleDescription] nvarchar (100)
,[IsDeleted] bit
) 
AS 
BEGIN 

-- Create table variable to hold history records.
Declare @AuditDataExtract table
	(
	AuditDate datetime
	,Operation varchar(1)
	,[RowVersion] int
	,[RoleName] varchar(50)
	,[RoleID] varchar(50)
	,[RoleDescription] varchar(50)
	,[IsDeleted] varchar(50)
	) 

-- Detailed data retrieval is enabled for this table.
--write the full history for the table into a temp table variable for performance.
Insert @AuditDataExtract 
Select 
			AuditDate
			,Operation
			,[RowVersion]
			,[RoleName]
			,[RoleID]
			,[RoleDescription]
			,[IsDeleted]
FROM		[App].[vApplicationRole_RowHistory]

-- Detailed data retrieval is enabled for this table.
;With	AuditDataExtract --Source data query
	AS
	(
	SELECT		*
	FROM		@AuditDataExtract
	),
	CurrentRowExtract 
	AS
	(
	Select		getdate() as AuditDate
				,'c' as Operation
				,(Select isnull(max([RowVersion]),0) + 1 from AuditDataExtract 
				Where	1=1 
				and	AuditDataExtract.[RoleName] = [App].[ApplicationRole].[RoleName]
				) as [RowVersion]
				,[RoleName]
				,cast([RoleID] as varchar(50)) as [RoleID]
				,cast([RoleDescription] as varchar(50)) as [RoleDescription]
				,cast([IsDeleted] as varchar(50)) as [IsDeleted]
	from		[App].[ApplicationRole]
			),
			RowHistoryExtract
			AS
			(
			Select * from AuditDataExtract
			Union All
			Select * from CurrentRowExtract
			),
			MostRecentRows
			AS
			(
			--Get most recent rows
			Select 
						max([RowVersion]) as MostRecentRow
						,[RoleName]
			from		RowHistoryExtract
			Group By	
						[RoleName]
			),
			RowHistory
			AS
			(
			--Anchor query for RowHistory buildup. Get the most current rowversion
			Select		RowHistoryExtract.*
			From		RowHistoryExtract
			inner join	MostRecentRows
				on		RowHistoryExtract.[RowVersion] = MostRecentRows.MostRecentRow
				and		RowHistoryExtract.[RoleID] = MostRecentRows.[RoleID]
			UNION All
			--Recursive query for RowHistory buildup
			Select		NextVersion.AuditDate
						,NextVersion.Operation
						,NextVersion.[RowVersion]
						,isnull(NextVersion.[RoleName] , PreviousVersion.[RoleName])
						,isnull(NextVersion.[RoleID] , PreviousVersion.[RoleID])
						,isnull(NextVersion.[RoleDescription] , PreviousVersion.[RoleDescription])
						,isnull(NextVersion.[IsDeleted] , PreviousVersion.[IsDeleted])
			from		RowHistoryExtract as NextVersion
			Inner join	RowHistory as PreviousVersion
				on		PreviousVersion.[RowVersion] = NextVersion.[RowVersion] + 1
				and		PreviousVersion.[RoleID] = NextVersion.[RoleID]
			),
			RowsOfInterest
			AS
			(
			--Get Rows Of Interest
			Select		max([RowVersion]) as [RowVersion]
						,[RoleName]
			from		RowHistory
			Where		[AuditDate] <= @RecoveryTime
				And		Operation <> 'c'
			Group By		
						[RoleName]
			)
-- Statement that executes the CTE
Insert into @HistoryData
--Returns the function table
Select 
			nullif(rh.[RoleID],'<-null->')
			,rh.[RoleName]
			,nullif(rh.[RoleDescription],'<-null->')
			,nullif(rh.[IsDeleted],'<-null->')
FROM		RowHistory as rh
Inner join	RowsOfInterest as roi
	on		rh.[RowVersion] = roi.[RowVersion]
	and		rh.[RoleID] = roi.[RoleID]
Where		Operation <> 'd'
order by 
			rh.[RoleID],
			rh.[RowVersion],
			rh.AuditDate
option		(MAXRECURSION 10000)

Return 
END 