問題描述
我在 T-SQL 中有這個語句
I have this statement in T-SQL
[dateReturned] [DATE] NULL
CONSTRAINT [Date_Returned] CHECK (dateReturned >= dateRented),
但是當我執行整個查詢時,我收到了這個錯誤.
but when I execute the whole query I am getting this error.
消息 8141,級別 16,狀態 0,第 17 行
dateReturned"列的 CHECK 約束引用了另一列,即Rental"表.
消息 1750,級別 16,狀態 0,第 17 行
無法創建約束.查看以前的錯誤.
Msg 8141, Level 16, State 0, Line 17
Column CHECK constraint for column 'dateReturned' references another column, table 'Rental'.
Msg 1750, Level 16, State 0, Line 17
Could not create constraint. See previous errors.
聲明有什么問題?
推薦答案
必須在表級別定義多列 CHECK 約束.在列級別定義的約束不能引用其他列
Multi-column CHECK constraints must be defined at the table level. Constraints defined at the column level can't reference other columns
來自文檔:
您可以對單個列應用多個 CHECK 約束.
You can apply multiple CHECK constraints to a single column.
您還可以通過在表級別創建單個 CHECK 約束來將其應用于多個列.
You can also apply a single CHECK constraint to multiple columns by creating it at the table level.
取自 另一個 MSDN 頁面:
ALTER TABLE dbo.Vendors ADD CONSTRAINT CK_Vendor_CreditRating
CHECK (CreditRating >= 1 AND CreditRating <= 5)
這篇關于SQL Server 日期列不大于另一個日期列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!