

USE AdventureWorks
go
TRUNCATE Table dbo.Audit
go

EXEC pAutoAudit 'HumanResources', 'Department'
EXEC pAutoAudit 'Production', 'Culture'

DECLARE @PK INT


-- Inserts
INSERT Production.Culture (CultureID, Name)
  Values ('x1', 'Test One')
INSERT Production.Culture (CultureID, Name)
  Values ('x2', 'Alien Culture')
INSERT Production.Culture (CultureID, Name)
  Values ('x3', 'Dino Culture')
INSERT Production.Culture (CultureID, Name)
  Values ('x4', 'Fourth Nation')

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

SET @PK =  Scope_Identity()

-- Updates
UPDATE HumanResources.Department
  SET GroupName = 'IT/Planning 2'
  WHERE DepartmentID = 1

UPDATE HumanResources.Department
  SET Name = 'DB Team'
  WHERE DepartmentID = @PK

UPDATE Production.Culture
  SET Name = 'New Name' 
  WHERE CultureID = 'x1'
UPDATE Production.Culture
  SET Name = 'Newer Name' 
  WHERE CultureID = 'x1'
UPDATE Production.Culture
  SET Name = 'Newest Name' 
  WHERE CultureID = 'x1'

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

DELETE Production.Culture
  WHERE CultureID LIKE 'x%'

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

-- audit Culture: x1 row
SELECT * 
  FROM dbo.Audit
  WHERE TableName = 'Production.Culture'
    AND PrimaryKey = 'x1'

-- view all  of audit trail
Select * from dbo.Audit

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

-- reconstruct deleted Culture rows 
Select * from Production.vCulture_Deleted




