insert and update-Trigger

  • Hi,

    I would like to create a trigger for insert and update whenever change the table or a record is added.

    Can i realise this with one Trigger ?

    Thank You !!

    🙂

    Best Regards

    Nicole

  • The best way to learn is to try it for yourself. Report back on what you find.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Hi Nicole,

    You just have to search Help using "Trigger" as a keyword and you'll see all of the types of triggers that you can create... 🙂

  • Simple example:

    USE tempdb

    GO

    CREATE TABLE dbo.Test

    (

    pk INTEGER PRIMARY KEY,

    data MONEY NOT NULL,

    change_date DATETIME2 NULL

    )

    GO

    CREATE TRIGGER dbo.Test_IU ON dbo.Test

    AFTER INSERT, UPDATE

    AS

    BEGIN

    SET ROWCOUNT 0

    SET NOCOUNT ON

    UPDATE t

    SET change_date = SYSDATETIME()

    FROM dbo.Test AS t

    JOIN inserted AS i ON

    i.pk = t.pk

    END

    INSERT dbo.Test (pk, data) VALUES (1, $10.37)

    SELECT * FROM dbo.Test AS t

    UPDATE dbo.Test SET data += $4.53 WHERE pk = 1

    SELECT * FROM dbo.Test AS t

    GO

    DROP TABLE dbo.Test

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply