SQL server检查空值

SQL server检查空值,sql,Sql,我有一个表,我正在添加一个约束,因此如果PaymentType不同于“Check”,请确保CheckNumber为空。这是我的桌子: create table RegistrationHeader( RegistrationNo numeric ,BillingID varchar(30) unique not null ,RegistrationDate date not null ,PaymentType varchar(5) check (PaymentT

我有一个表,我正在添加一个约束,因此如果PaymentType不同于“Check”,请确保CheckNumber为空。这是我的桌子:

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') or (CheckNumber is null))
如果要确保检查的
CheckNumber
也不为空:

constraint CheckNumber_type_constr
    check ((PaymentType = 'Check' and CheckNumber is not null) or
           (PaymentType <> 'Check' and CheckNumber is null)
          )
constraint CheckNumber\u type\u constr
支票((PaymentType='check'且支票编号不为空)或
(PaymentType“Check”和CheckNumber为空)
)
constraint CheckNumber_type_constr
    check ((PaymentType = 'Check' and CheckNumber is not null) or
           (PaymentType <> 'Check' and CheckNumber is null)
          )