简体   繁体   English

SQL Server datecolumn不能大于另一个日期列

[英]SQL Server datecolumn NOT greater than another date column

I have this statement in T-SQL 我在T-SQL中有此声明

[dateReturned] [DATE] NULL 
   CONSTRAINT [Date_Returned] CHECK (dateReturned >= dateRented),

but when I execute the whole query I am getting this error. 但是当我执行整个查询时,我得到了这个错误。

Msg 8141, Level 16, State 0, Line 17 消息8141,级别16,状态0,第17行
Column CHECK constraint for column 'dateReturned' references another column, table 'Rental'. 列“ dateReturned”的列CHECK约束引用了表“出租”的另一列。
Msg 1750, Level 16, State 0, Line 17 消息1750,级别16,状态0,第17行
Could not create constraint. 无法创建约束。 See previous errors. 请参阅先前的错误。

What is wrong with the statement? 声明有什么问题?

Multi-column CHECK constraints must be defined at the table level. 必须在表级别定义多列CHECK约束。 Constraints defined at the column level can't reference other columns 在列级别定义的约束不能引用其他列

From the documentation : 文档中

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. 您还可以通过在表级别创建单个CHECK约束来将其应用于多列。

Taken from another MSDN page : 取自另一个MSDN页面

ALTER TABLE dbo.Vendors ADD CONSTRAINT CK_Vendor_CreditRating
CHECK (CreditRating >= 1 AND CreditRating <= 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM