为“插入前"创建触发器. [英] Create trigger for "before insert"

查看:51
本文介绍了为“插入前"创建触发器.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为一个将在 INSERT 之前使用的表创建触发器.

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

我想检查两列是否为NULL,并且是否为 NULL ,否则将引发错误,否则将对数据进行 INSERT .

I want to check if two columns are NULL and if they are NULL raise an error otherwise INSERT the data.

到目前为止,我有这个:

So far I have this:

CREATE TRIGGER INS_TABLE_1
ON mytable
FOR INSERT
AS
BEGIN

    IF (column1 IS NULL OR column2 IS NULL)
    BEGIN
        RAISERROR ('You are not allowed to Add These Data.', 10, 11)
    END
    ELSE
        INSERT INTO mytable (column1,column2,column3)
END

你能帮我吗?

推荐答案

使用而不是触发器插入的表在 INSERT UPDATE 语句期间存储受影响的行的副本.代替触发器将当前的INSERT替换为触发器定义.

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

这篇关于为“插入前"创建触发器.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆