
USE AdventureWorks2008
go
TRUNCATE TABLE dbo.Audit
TRUNCATE TABLE dbo.SchemaAudit
go

EXEC pAutoAudit 'HumanResources', 'Department'

go 

ALTER TABLE HumanResources.Department
  ADD TestCol INT
  
go 

DECLARE @PK INT

INSERT HumanResources.Department  (Name, GroupName)
  VALUES('DBA', 'IT' )

SET @PK =  Scope_Identity()

-- Updates

UPDATE HumanResources.Department
  SET Name = 'DB Team 1'
  WHERE DepartmentID = @PK
  
waitfor delay '00:00:00.1'  

UPDATE HumanResources.Department
  SET Name = 'Data Group'
  WHERE DepartmentID = @PK

waitfor delay '00:00:00.1'
  
  UPDATE HumanResources.Department
  SET GroupName = 'IT/Dev ',
    Name = 'Db Dev'
  WHERE DepartmentID = @PK
  
---------------------------------------------------
-- Reconstruct RowHistory
Select * FROM HumanResources.Department_RowHistory (@PK)

----------------------------------------------------
-- Deletes
DELETE HumanResources.Department
  WHERE DepartmentID = @PK

-- Reconstruct deleted Department rows
Select * from HumanResources.vDepartment_Deleted

----------------------------------------------------
-- Examine Raw Audit Trail
SELECT * 
  FROM dbo.Audit
  WHERE TableName = 'HumanResources.Department'
    AND PrimaryKey = @PK


go 

-- cleanup 
ALTER TABLE HumanResources.Department
  DROP COLUMN TestCol
