[英]SQL - Update, Insert Or Instead of Trigger?
In case of updating or inserting value to one of the fields in my table, I would like to check that the value does not contain some chars. 如果将值更新或插入表中的字段之一,我想检查该值是否不包含某些字符。 If it does, don't insert nothing.
如果是这样,请勿插入任何内容。 I did this "Instead of" trigger, but it worked only for inserted rows and not for updated rows .
我做了这个“代替”触发器, 但它仅适用于插入的行,不适用于更新的行 。 What did I do wrong?
我做错了什么? Did I choose the right trigger for this task?
我是否为此任务选择了合适的触发器?
ALTER TRIGGER trg_MakeSureOfCleanDescription
ON [dbo].[Car]
instead OF INSERT
AS
BEGIN
DECLARE @CharPosition_Inserted INT;
SET @CharPosition_Inserted = 0;
DECLARE @CharPosition_Updated INT;
SET @CharPosition_Updated = 0;
SELECT @CharPosition_Inserted = CHARINDEX('<', inserted.Description)
FROM inserted
--select @CharPosition_Updated = CHARINDEX('<', inserted.Description) from
IF( @CharPosition_Inserted = 0/*and @CharPosition_Updated = 0*/ )--only if the description is html free, insert it. Otherwise don't insert nothing.
BEGIN
PRINT 'no html. inserting.'
INSERT INTO Car
([Year],
[Make],
[Model],
[Price],
[ExtColor],
[IntColor],
[Body],
[Engine],
[Transmission],
[Miles],
[VIN],
[Description],
[MainImage],
[VideoFile],
[StockNumber],
[Sold])
SELECT [Year],
[Make],
[Model],
[Price],
[ExtColor],
[IntColor],
[Body],
[Engine],
[Transmission],
[Miles],
[VIN],
[Description],
[MainImage],
[VideoFile],
[StockNumber],
[Sold]
FROM inserted
END
END
Thanks. 谢谢。
My code: 我的代码:
you are only checking for the insert action. 您仅在检查插入操作。 The update is treated totally differently and has to be handled in a separate trigger.
更新的处理方式完全不同,必须在单独的触发器中进行处理。 As there are Before and After triggers for insert, so there are before and after triggers for Update and Delete as well.
由于存在用于插入的Before和After触发器,因此也存在用于Update和Delete的Before和After触发器。 In your case you need to create another trigger for the update scenario.
在您的情况下,您需要为更新方案创建另一个触发器。
https://technet.microsoft.com/en-us/library/ms188601%28v=sql.105%29.aspx https://technet.microsoft.com/zh-cn/library/ms188601%28v=sql.105%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.