We can call user defined function in a constraint. This is very helpful when we have to add similar kind of constraint on many tables.
Let’s take an example here
Create a function
CRAETE FUNCTION [TestFucntion] (@Salary nvarchar(50))
DECLARE @Exists BIT
SET @Exists =0
SET @Exists =1
Execute the above script and it will create a function which can be seen in object explorer of the selected database, under the function folder.
Create a table employee
Create table Employee (ID int, Salary money)
Add a testConstraint on table employee
Alter TABLE Employee
WITH NOCHECK ADD CONSTRAINT [testConstraint]
The above script will add constraint on the table Employee and will not allow entering salary more than 1500.
Thus we can do any other activity here in function and can write complex constraint for table easily.