Check constraint fail while updating row

  • Hi,

    i apply constraint to check two columns values.below is my table structure

    priority1 priority2 priority3 priority_hashvalue

    my check constraint is

    [priority_hashvalue]=hashbytes('md5',([priority1]+[priority2]+[priority3]))

    it's working fine while inserting new record into table.mismatched while updating record.

    priority_hashvalue generate using priority1,priority2,priority3 values.

    record updated successfully when pass old values as it is.

    thanks in advance.

    --chalam

  • chalam87 (6/23/2014)


    Hi,

    i apply constraint to check two columns values.below is my table structure

    priority1 priority2 priority3 priority_hashvalue

    my check constraint is

    [priority_hashvalue]=hashbytes('md5',([priority1]+[priority2]+[priority3]))

    it's working fine while inserting new record into table.mismatched while updating record.

    priority_hashvalue generate using priority1,priority2,priority3 values.

    record updated successfully when pass old values as it is.

    thanks in advance.

    Priority_hashvalue is a calculated column?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Priority_hashvalue genereate like below

    insert into testtable(priroty1,priroty2,priroty3,Priority_hashvalue) values(@priroty1,@priroty2,@priroty3,hashbytes('md5',(@priroty1+@priroty2+priroty3)))

    --chalam

  • chalam87 (6/23/2014)


    Priority_hashvalue genereate like below

    insert into testtable(priroty1,priroty2,priroty3,Priority_hashvalue) values(@priroty1,@priroty2,@priroty3,hashbytes('md5',(@priroty1+@priroty2+priroty3)))

    So it is hardcoded into the table.

    Ify you change the data in one of the priority columns, the checksum becomes different than the one stored in priority_hashvalue, so it is expected that the constraint will fail.

    I would either make priority_hashvalue a calculated column, or update priority_hashvalue as well when you update another column.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • hi,

    updated priroty_hashvalue column also like below but it's constraint violated

    update testtable set priroty1=@priroty1,

    priroty2=@priroty2,

    priroty3=@priroty3,

    priroty_hashvalue=hashbytes('md5',@priroty1+@priroty2+@priroty3) where clause.

    --chalam

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply