all groups > sql server programming > july 2007 >
You're in the

sql server programming

group:

Field has higher value than another field


Field has higher value than another field scuba79
7/18/2007 6:34:01 PM
sql server programming: I trying to create a table that will hold mileage of a vehicle.

create table vehiclelog
(
DateOfUsage [Datetime] not null default (getdate()),
StartingMileage [int] not null,
EndingMileage [int] not null
)

When a user enters information in the table, is there a way to check to make
sure that the ending mileage is higher than the starting mileage and if it
isn't to throw an error?

Re: Field has higher value than another field scuba79
7/18/2007 7:38:00 PM
Thanks... I forgot to add that field, I created the table in a hurry when
writing the question.

[quoted text, click to view]
Re: Field has higher value than another field Roy Harvey
7/18/2007 9:51:02 PM
Yes, it is quite straightforward.

ALTER TABLE vehiclelog
ADD CONSTRAINT StartBeforeEnd_check CHECK (StartingMileage <
EndingMileage)

On an unrelated note, I was a bit surprised that the table didn't have
any sort of vehicle identifier column.

Roy Harvey


On Wed, 18 Jul 2007 18:34:01 -0700, scuba79
[quoted text, click to view]
AddThis Social Bookmark Button