Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Dealing with Auditing data - is there a cleaner way?

$
0
0

Hello all,

I'm working on a report to make sense of an audit trail.

The audit trail is implemented is two tables, one has a row for each 'transaction' the other has a row for each column changed in that 'transaction' with a new value old value pair (so every column is there on insert/delete but only changed columns on update).

Below is what I have so far - the output at the end is what I'm looking for. For some reason the PIVOT syntax has just never really 'clicked' with me so I'm not sure if that is something I should be making use of in this situation or if I'm on the right track with CASE

CREATE TABLE #AuditTransaction 
    (
    Transaction_ID INT PRIMARY KEY,
    Modified_Date DATETIME NOT NULL,
    [User_ID] INT NOT NULL,
    [Action] VARCHAR(1) NOT NULL,
    Table_Name VARCHAR(20) NOT NULL,
    Affected_Rows INT NOT NULL,
    )

CREATE TABLE #AuditData
    (
    Data_ID INT PRIMARY KEY,
    Transaction_ID INT REFERENCES #AuditTransaction(Transaction_ID),
    Primary_Key VARCHAR(50),
    Column_Name VARCHAR(50),
    New_Value VARCHAR(250),
    Old_Value VARCHAR(250)
    )
    
INSERT INTO #AuditTransaction VALUES (1, '20130618', 123, 'I', 'ClaimDetails', 3)
INSERT INTO #AuditTransaction VALUES (2, '20130619', 456, 'U', 'ClaimDetails', 2)
INSERT INTO #AuditTransaction VALUES (3, '20130620', 123, 'D', 'ClaimDetails', 1)

INSERT #AuditData VALUES (1, 1, 'claim_id=12345', 'claim_property', '1 Paper Road', NULL)
INSERT #AuditData VALUES (2, 1, 'claim_id=12345', 'Valid', '1', NULL)
INSERT #AuditData VALUES (3, 1, 'claim_id=12345', 'Amount', '12345', NULL)

INSERT #AuditData VALUES (4, 1, 'claim_id=12346', 'claim_property', '2 Paper Road', NULL)
INSERT #AuditData VALUES (5, 1, 'claim_id=12346', 'Valid', '1', NULL)
INSERT #AuditData VALUES (6, 1, 'claim_id=12346', 'Amount', '12345', NULL)

INSERT #AuditData VALUES (7, 1, 'claim_id=12347', 'claim_property', '1 Some Other Street', NULL)
INSERT #AuditData VALUES (8, 1, 'claim_id=12347', 'Valid', '1', NULL)
INSERT #AuditData VALUES (9, 1, 'claim_id=12347', 'Amount', '12345', NULL)

INSERT #AuditData VALUES (10, 2, 'claim_id=12346', 'claim_property', '2 Some Other Street', '2 Paper Road')

INSERT #AuditData VALUES (15, 2, 'claim_id=12347', 'Amount', '0', '12345')

INSERT #AuditData VALUES (16, 3, 'claim_id=12345', 'claim_property', NULL, '1 Paper Road')
INSERT #AuditData VALUES (17, 3, 'claim_id=12345', 'Valid', NULL, '1')
INSERT #AuditData VALUES (18, 3, 'claim_id=12345', 'Amount', NULL, '12345')

INSERT #AuditData VALUES (19, 3, 'claim_id=12346', 'claim_property', NULL, '2 Some Other Street')
INSERT #AuditData VALUES (20, 3, 'claim_id=12346', 'Valid', NULL, '1')
INSERT #AuditData VALUES (21, 3, 'claim_id=12346', 'Amount', NULL, '12345')

-- This is my current query
SELECT
    at.Transaction_ID,
    at.Table_Name,
    at.[Action],
    at.Modified_Date,
    at.[User_ID],
    ad.Primary_Key,
    MAX(CASE WHEN ad.Column_Name = 'Claim_Property' THEN New_Value END) AS Claim_Property_New,
    MAX(CASE WHEN ad.Column_Name = 'Claim_Property' THEN Old_Value END) AS Claim_Property_Old,
    MAX(CASE WHEN ad.Column_Name = 'Valid' THEN New_Value END) AS Valid_New,
    MAX(CASE WHEN ad.Column_Name = 'Valid' THEN Old_Value END) AS Valid_Old,
    MAX(CASE WHEN ad.Column_Name = 'Amount' THEN New_Value END) AS Amount_New,
    MAX(CASE WHEN ad.Column_Name = 'Amount' THEN Old_Value END) AS Amount_Old
FROM #AuditTransaction at
JOIN #AuditData ad ON at.Transaction_ID = ad.Transaction_ID
WHERE at.Table_Name = 'ClaimDetails'
    AND at.Modified_Date > '20130601'
GROUP BY
    at.Transaction_ID,
    at.Table_Name,
    at.[Action],
    at.Modified_Date,
    at.[User_ID],
    ad.Primary_Key
ORDER BY
    at.Transaction_ID

DROP TABLE #AuditData
DROP TABLE #AuditTransaction

Thanks


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>