两个数据之间的情况

时间:2014-03-04 17:04:46

标签: sql constraints case between

嗨我必须用这种情况定义约束..

    [driver_surcharge_to] [tinyint] NULL CONSTRAINT CK_Vehicle_driverSurchargeTo CHECK (
        1 = case driver_surcharge_from
        when null
            case driver_age_to
                when null then 1
                else 0 end
        else
        case driver_age_to
                when null then 0
                when (driver_age_to) between 16 and 99 then 1
                else 0 end
    end),

我试图使用相同的名称字段,并且不使用或不使用它。(括号)

如何声明案例以验证他在两个数字之间的价值?

感谢。

1 个答案:

答案 0 :(得分:1)

您想使用case的“条件”形式:

[driver_surcharge_to] [tinyint] NULL CONSTRAINT CK_Vehicle_driverSurchargeTo CHECK (
    1 = case driver_surcharge_from
    when null
        case driver_age_to
            when null then 1
            else 0 end
    else
    case when driver_age_to is null then 0
         when driver_age_to between 16 and 99 then 1
         else 0 end
end),

我认为以下逻辑稍微简单一些:

[driver_surcharge_to] [tinyint] NULL CONSTRAINT CK_Vehicle_driverSurchargeTo CHECK (
    (driver_surcharge_from is null and driver_age_to is null) or
    (driver_surcharge_from is not null and driver_age_to between 16 and 99 )
)