Check Constraint : col1 Null if col2 Not Null

Hi everyone,

I can't found how I can do this,
It seems to me, a python script can not do that because it can not return a null value...

Is there a workaround or something that makes it possible?

Thanks

Answers

  • Alex BAlex B Posts: 1,151 Diamond 4
    Hi @RomainH,

    You can return DBNull like this to return NULL
    return DBNull

    But I couldn't get a check for a row value of NULL to work.  I tried
    val is None
    val is not None
    val is DBNull
    val is DBNull.Value
    val == DBNull
    val == DBNull.Value
    But couldn't get it to work.

    If I did 
    def main(config):
            if (col2 == "notnull   "):
            return 4
        else:
            return DBNull
        return 0<br>
    Then it would correctly place null or the number 4


    But that required me to know the value of col2 to check against.

    So I'm not sure what might work here, perhaps you have solved that part of it and just returning DBNull will get you there, if so please let us know!

    Kind regards,
    Alex
    Product Support Engineer | Redgate Software

    Have you visited our Help Center?
  • pdbpdb Posts: 1 New member
    edited January 12, 2023 10:29AM
    Alex B said:

    But I couldn't get a check for a row value of NULL to work.  I tried
    val is None
    val is not None
    val is DBNull
    val is DBNull.Value
    val == DBNull
    val == DBNull.Value
    But couldn't get it to work.

    Hello, I am struggeling with the same issue. Are there any updates on "how to check another column value for null within a python script"?

    EDIT: Finally found out myself. We need to use
    val.IsNull
    Found it by accident. Where is a documentation for that?
Sign In or Register to comment.