Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server datecolumn NOT greater than another date column

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.

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.

What is wrong with the statement?

like image 204
JackyBoi Avatar asked Mar 16 '26 10:03

JackyBoi


1 Answers

Multi-column CHECK constraints must be defined at the table level. Constraints defined at the column level can't reference other columns

From the documentation :

You can apply multiple CHECK constraints to a single column.

You can also apply a single CHECK constraint to multiple columns by creating it at the table level.

Taken from another MSDN page:

ALTER TABLE dbo.Vendors ADD CONSTRAINT CK_Vendor_CreditRating
CHECK (CreditRating >= 1 AND CreditRating <= 5)
like image 111
Panagiotis Kanavos Avatar answered Mar 19 '26 02:03

Panagiotis Kanavos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!