0

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
25
  • I'm not sure what you're looking for, can you elaborate on exactly what it is you need help with? i.e. What's stopping you for doing this yourself ? Commented Jan 22, 2018 at 13:44
  • So the measure_slave table has various fields that could be changed by a user and this trigger records an entry for those changes, however I want to output a message depending on which were changed. At the moment I've hard-typed the change in there but I cannot get the trigger to accept CASE WHEN xx SET @message = 'Message' Commented Jan 22, 2018 at 13:53
  • I've updated the question to illustrate what I'm trying to get to... Commented Jan 22, 2018 at 13:59
  • so what's wrong with your trigger query ? Commented Jan 22, 2018 at 14:07
  • why is there -1 after each i.slave_PK in the where clause of your subqueries ? Commented Jan 22, 2018 at 14:12

1 Answer 1

2

I think you may be able to achive what you need without using a variable:

ALTER TRIGGER [dbo].[trg_Measure_Insert_Audit] 
   ON [dbo].[Measures_slave]
   AFTER INSERT
AS 
BEGIN
SET NOCOUNT ON;

    INSERT INTO aud_Measures
            (
            measure_FK,
            [description],
            oldname,
            [newname],
            milestone
            )

    SELECT  
            i.measure_PK,
            CASE WHEN (i.measure_name <> s.measure_name) THEN 'Measure Name Changed to <b> ' + i.measure_name + ' </b>' ELSE '' END +             -- The resulting value from this line concatenates with 
            CASE WHEN (i.calculation_steps  <> s.calculation_steps ) THEN 'Steps changed to:  <b> ' + i.calculation_steps + ' </b>' ELSE '' END,  -- the resulting value from of this line
            s.measure_name,
            i.measure_name 
            'Yes' 
    FROM Inserted i LEFT JOIN Measures_slave s ON s.slave_PK = i.slave_PK -1
    -- If you want to audit only the rows that in which there IS actually a difference in any of measure_name  or calculation_steps columns, then use the following:
    WHERE (i.measure_name <> s.measure_name) OR (i.calculation_steps  <> s.calculation_steps )
END
Sign up to request clarification or add additional context in comments.

2 Comments

Emilio perfect! This absolutely works exactly how I need it to. So simple but I was clearly making it more complicated in my head. Thank you
That's great. Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.