Create trigger for “before insert”

前端 未结 1 1746
春和景丽
春和景丽 2021-01-26 04:28

I want to make a trigger for one table that will be used before INSERT.

I want to check if two columns are NULL and if they are NULL raise an e

1条回答
  •  礼貌的吻别
    2021-01-26 05:06

    Use instead trigger and inserted table like below and have a try.

    CREATE TRIGGER INS_TABLE_1
    ON mytable
    INSTEAD OF INSERT
    AS
    BEGIN
        DECLARE @fn varchar(50),@ln varchar(50)
        SELECT @fn=column1 ,@ln=column12 from inserted
        IF (@fn IS NULL OR @ln IS NULL)
        BEGIN
            RAISERROR ('You are not allowed to Add These Data.', 10, 11)
        END
        ELSE
            INSERT INTO mytable (column1 ,column2) values (@fn,@ln)
    END
    

    The inserted table stores copies of the affected rows during INSERT and UPDATEstatements. Instead of trigger replaces the current INSERT by the trigger definition.

    0 讨论(0)
提交回复
热议问题