简体   繁体   English

在 SQL 中触发检查插入是否包含列的值

[英]In SQL Trigger check if an Insert contains a value for a column

I do have a trigger in my SQL table and I'm trying to check whether or not an insert contains a value for column Produktnummer .我的 SQL 表中确实有一个触发器,我正在尝试检查插入是否包含列Produktnummer的值。 My reasoning behind this is whether or not to run another insert which would be caused by the trigger.我的理由是是否运行另一个由触发器引起的插入。

I do have this inside my trigger before the actual insert my trigger would cause.在我的触发器实际插入之前,我的触发器中确实有这个。 IF NEW.Produktnummer IS NOT NULL OR NOT '' THEN followed by END IF at the end. IF NEW.Produktnummer IS NOT NULL OR NOT '' THEN后跟END IF最后。

My trigger looks like this:我的触发器如下所示:

IF NEW.Produktnummer IS NOT NULL OR NOT '' THEN
[This piece triggers another Insert]
END IF

But It seems like the if expression is always true even when my insert didn't even contain a value for NEW.Produktnummer .但似乎 if 表达式总是正确的,即使我的插入甚至不包含NEW.Produktnummer的值。

Can I even check for IS NOT NULL OR NOT '' with NEW variables inside triggers or is there another way to conditionally run the statement inside the trigger?我什至可以使用触发器内的新变量检查IS NOT NULL OR NOT ''还是有另一种方法可以有条件地在触发器内运行语句?

'' is a false-y in a boolean context, so NOT '' is true, and the if condition is always true. ''在 boolean 上下文中是 false-y,因此NOT ''为真,并且if条件始终为真。 It looks like you meant to check Produktnummer with a <> operator:看起来您打算使用<>运算符检查Produktnummer

IF NEW.Produktnummer IS NOT NULL AND NEW.Produktnummer <> '' THEN
    -- trigger the other insert
END IF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM