I have a trigger set on a table to run after Insert and I want to query the incoming data with that already in the target table, then append a different message based on the changes identified.
Here's what I have so far
ALTER TRIGGER [dbo].[trg_Measure_Insert_Audit]
ON [dbo].[Measures_slave]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @message VARCHAR(MAX)
insert into aud_Measures
(
measure_FK,
[description],
oldname,
[newname],
milestone
)
select
i.measure_PK,
CASE
WHEN (SELECT TOP 1 measure_name FROM Measures_slave s WHERE s.slave_PK = i.slave_PK -1) <> i.measure_name THEN 'Measure Name Changed to <b> ' + i.measure_name + ' </b>'
END,
(SELECT TOP 1 measure_name FROM Measures_slave s WHERE s.slave_PK = i.slave_PK -1),
i.measure_name,
--'Test Audit Entry',
'Yes'
from
inserted i
END
I want to replace the CASE WHEN with a list of either IFs or WHENs that go down each field on both measure_slave and Inserted and append a message to @message if a difference is found.
I'm trying to achieve this, essentially:
select
i.measure_PK,
CASE
--WHEN (SELECT TOP 1 measure_name FROM Measures_slave s WHERE s.slave_PK = i.slave_PK -1) <> i.measure_name THEN 'Measure Name Changed to <b> ' + i.measure_name + ' </b>'
WHEN (SELECT TOP 1 measure_name FROM Measures_slave s WHERE s.slave_PK = i.slave_PK -1) <> i.measure_name SET @message = 'Measure Name Changed to <b> ' + i.measure_name + ' </b>'
WHEN (SELECT TOP 1 calculation_steps FROM Measures_slave s WHERE s.slave_PK = i.slave_PK -1) <> i.calculation_steps SET @message = @message + 'Steps changed to: <b> ' + i.calculation_steps + ' </b>'
END,
(SELECT TOP 1 measure_name FROM Measures_slave s WHERE s.slave_PK = i.slave_PK -1),
i.measure_name,
--'Test Audit Entry',
'Yes'
from
inserted i
-1after eachi.slave_PKin the where clause of your subqueries ?