3

我正在使用SQL Server 2008,我有一個包含三列的表:Length,StartTimeEndTime。我想就這個表,說CHECK約束:SQL CHECK約束問題

if Length == NULL then 
    StartTime <> NULL and EndTime <> NULL 
else 
    StartTime == NULL and EndTime == NULL 

我已經開始嘗試這樣的事情:

Length == NULL AND StartTime <> NULL AND EndTime <> NULL 

這顯然是不夠的,但即使這樣簡單的表達式不驗證。我得到的錯誤:

"Error validating 'CK_Test_Length_Or_Time'. Do you want to edit the constraint?"

任何想法如何去做到這一點?

回答

3

SQL Server中沒有==運算符。雖然檢查空,你必須用 「是」

請試試這個:

((Length is null AND starttime is not null AND endtime is not null) OR 
(Length is not null AND starttime is null AND endtime is null)) 

HTH

4
CHECK ([Length] IS NULL AND [StartTime] IS NOT NULL AND [EndTime] IS NOT NULL 
     OR [Length] IS NOT NULL AND [StartTime] IS NULL AND [EndTime] IS NULL))