Home Forums SQL Server 2012 SQL Server 2012 - T-SQL Check constraint where all records with the same effective date add up to one RE: Check constraint where all records with the same effective date add up to one

  • Another option is to make use of an indexed view/materialized view to implement the logic(sum of all percentage to be > 100%) and have a unique clustered index on it.
    So if the rule did NOT violate the <=100% rule the indexed view would return nothing and if it did the unique clustered index would throw the error.

    Check the link which shows the details of doing this along with the caveats
    https://spaghettidba.com/2011/08/03/enforcing-complex-constraints-with-indexed-views/

    I haven't tested this myself so not sure if this is going to get called per row insert basis in which case it is not going to help.