Forum Discussion

AjcbutlerSeiki's avatar
AjcbutlerSeiki
Copper Contributor
Apr 11, 2025
Solved

Error when trying to create a Trigger

Hi

I am trying to create a trigger so when a value changes from null to a value it copies the value into another field.  However, I am getting errors on the basic code like BEFORE.

Any ideas why?

Thanks

Aaron

  • Hi

    I managed to resolve the issue.  I think it was because the code I was using was mySQL and when I converted it to TSQL it works.

    Thanks for trying to help.

    Aaron

4 Replies

  • olafhelper's avatar
    olafhelper
    Bronze Contributor

    Your SQL code is PL/SQL from/for Oracle database; don't work in T-SQL

  • AjcbutlerSeiki's avatar
    AjcbutlerSeiki
    Copper Contributor

    Hi

    I managed to resolve the issue.  I think it was because the code I was using was mySQL and when I converted it to TSQL it works.

    Thanks for trying to help.

    Aaron

  • divyed's avatar
    divyed
    Copper Contributor

    Hello ,

    This is not a valid syntax in SQL SERVER, looks like you're attempting more like MySQL or Oracle PL/SQL.

    The BEFORE trigger and NEW-OLD pseudo records do not exist in SQL Server.

    Here is a code in sql server you can use :

    USE CORE_DATA;
    GO

    CREATE TRIGGER trg_copy_two_fields
    ON dbo.OR_OP
    AFTER UPDATE
    AS
    BEGIN
        SET NOCOUNT ON;

        UPDATE t
        SET FIXSTIME = i.STIME
        FROM dbo.OR_OP t
        INNER JOIN inserted i ON t.PrimaryKey = i.PrimaryKey
        INNER JOIN deleted d ON d.PrimaryKey = i.PrimaryKey
        WHERE d.STIME IS NULL AND i.STIME IS NOT NULL;
    END
    GO

    Replace PrimaryKey with your actual primary key column.

    To copy ETIME to FIXETIME, add below code

        SET FIXSTIME = i.STIME,
            FIXETIME = i.ETIME

     

    I hope this will answer your query, if not let me know more about tool you are using.

     

    Kind Regards,

Resources