Posted by Prashant on December 3, 2012
This post describes how to use result of an user defined function with CHECK constraint in SQL Server. For demonstration, considered a situation where it is not allowed to insert or update records where calculated age of a person is less than 18 years as per his/her Date of Birth.
So for this first we need to create the function before creating the CHECK constraint. Here is a function which will return age as per the date of birth provided.
Now create a table where CHECK constraint will refer to this function to check if the age of the person meets the required criteria or not (minimum 18 Years in this case).
Now try to insert a record where calculated age is less than 18 years and see what happens.
As the age does not meet the required criteria in defined CHECK constraint, it doesn’t allow to insert this record…
View original post 32 more words