2014-03-04 69 views
0

嗨之間我必須確定這種情況下,約束..情況下,當兩個數據

[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

您要使用的 「條件」 形式的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) 
)