0
我有一個表,我添加一個約束,所以如果PaymentType不同於'檢查',以確保CheckNumber爲null。這是我的表:SQL服務器檢查爲空值
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)
)
);
所以,我已經試過
,constraint CheckNumber_type_constr check((PaymentType = 'CC' or PaymentType = 'PO') and CheckNumber is null)
和
,constraint CheckNumber_type_constr check((PaymentType != 'Check') and CheckNumber is null)
兩者慘遭失敗...