Skip to content
Advertisement

SQL – trigger select into after update/insert

i have a table called Audit_Data, and from time to time there is an update coming. Every single update consists of around 300 rows (around 20 columns per row), and all of the rows from the same update share the same Audit_ID.

I have a select, that pulls out only this data, which is relevant for me. It basically transform the 300×20 field data into one row of data.
Is there a way to create a SQL trigger, that would perform the select on the updated Audit_Data table, and insert selected data into table named Audit_Final?

This is my select statement that i use to pull out the relevant data:

SELECT main.Audit_ID
        ,main.Item_19
        ,main.Item_1
        ,main.Item_7
        ,main.Item_8
        ,Item_17
        ,main.Item_13
        ,macaddr.Item_2
        ,macaddr.Item_16
        ,t1.Item_1
FROM dbo.[Audit_Data] AS main
    LEFT JOIN
    (
        SELECT Audit_ID, Item_2, Item_16
        FROM dbo.[Audit_Data] AS macaddr
        WHERE 
        (Item_2 NOT LIKE 'Hyper-V%')
        AND (Item_17 = 'connected') 
        AND (Item_18 IN ('10000Mbps', '1000MBps') OR ITEM_9 IS NOT NULL AND ITEM_10 IS NOT NULL)
        AND (Item_18 != '100Mbps')
    ) macaddr ON main.Audit_ID = macaddr.Audit_ID
    LEFT JOIN
    (
        SELECT Audit_ID, Category_ID, Item_1, Record_ordinal
        FROM dbo.[Audit_Data] AS t1
        WHERE 
        Item_1 = 'Automatyczna konfiguracja sieci przewodowej' OR Item_1 = 'dot3svc' OR Item_1 = 'Wired AutoConfig'
        AND Item_3 = 'Running'
        AND Category_ID = '4100'
    ) t1 ON main.Audit_ID = t1.Audit_ID
WHERE 
main.Record_Ordinal = '2'
ORDER BY main.Audit_ID

Advertisement

Answer

Based on authors comment this is what is required here:

CREATE TRIGGER [TR_Audit_Data] ON [Audit_Data]
AFTER UPDATE
AS
 BEGIN
 INSERT INTO [Audit_Final](cloumn_1, colum_2, ... all columns you have on target table)
 /*
  Paste your select query here
 */
END
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement