简体   繁体   中英

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 . 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.

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 .

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 a false-y in a boolean context, so NOT '' is true, and the if condition is always true. It looks like you meant to check Produktnummer with a <> operator:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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