1CREATE FUNCTION dbo.fnIsAddressOwner (
2 @CustomerId int,
3 @AddressId int
4)
5RETURNS tinyint
6AS
7BEGIN
8 DECLARE @Result tinyint
9 IF EXISTS(SELECT * FROM CustomerAddresses WHERE CustomerId=@CustomerId and AddressId=@AddressId)
10 SET @Result= 1
11 ELSE
12 SET @Result= 0
13 RETURN @Result
14END
15
16
17CREATE TABLE Customers (
18 CustomerId int,
19 PreferredAddressId int,
20 CONSTRAINT ckPreferredAddressId CHECK (
21 dbo.fnIsAddressOwner(CustomerId, PreferredAddressId) = 1)
22 )
23)
24