I have a table and I am adding a constraint so if PaymentType is different than 'Check' to ensure that the CheckNumber is null. This is my table:
create table RegistrationHeader(
RegistrationNo numeric
,BillingID varchar(30) unique not null
,RegistrationDate date not null
,PaymentType varchar(5) check (PaymentType = 'CC' or PaymentType = 'PO' or PaymentType = 'Check') not null
,CCNumber varchar(16) check(LEN(CCNumber) = 16 OR LEN(CCNumber) = 15)
,PONumber varchar(30)
,CheckNumber varchar(10) default null
,primary key(RegistrationNo)
,constraint CC_CCNumber_constr check(
(PaymentType = 'CC' and CCNumber is not null)
or
(PaymentType != 'CC' and CCNumber is null)
)
,constraint PO_PONumber_constr check(
(PaymentType = 'PO' and (PONumber is not null or PONumber != ''))
or
(PaymentType != 'PO' and PONumber is null)
)
);
So, I have tried
,constraint CheckNumber_type_constr check((PaymentType = 'CC' or PaymentType = 'PO') and CheckNumber is null)
and
,constraint CheckNumber_type_constr check((PaymentType != 'Check') and CheckNumber is null)
and both fail miserably...