SQL Server 日期列不大于另一个日期列
我在 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)
相关文章