If you want a check constraint to reference another column in the table, you
must define it as a table level constraint and not as part of the column
definition. Your table definition can be made of column defintions (which
can include column level constraint) and of constraints outside of a column
definition, as a separate item in the table list. You must separate this
multicolumn constraint from the rest of the table definition with a comma:
create table box
(box_nu smallint primary key,
Box_sent_date smalldatetime
constraint ck_sent_date
check ((Box_sent_date) <= getdate()), -- this is column level constraint
on a single column; it could be
-- on
the same line asa the smalldatetime
Box_received_date smalldatetime, -- put a comma here to make the
following contraint be a separate element
constraint ck_received_date
check (box_received_date > box_sent_date))
As far as the insert error, SQL Server will interpret 9/9/1999 as a division
problem unless you put quotes around it. All datetime constants must be in
quotes.
HTH
--
Kalen Delaney
MCSE, SQL Server MCT, MVP
www.InsideSQLServer.com
Feed Someone for Free Today:
www.TheHungerSite.com
> --I'm trying to write a constraint that checks the value of another
> column in the same table.
> --This is what the book seems to want
> create table box
> (box_nu smallint primary key,
> Box_sent_date smalldatetime
> constraint ck_sent_date
> check ((Box_sent_date) <= getdate()),
> Box_received_date smalldatetime
> constraint ck_received_date
> check (box_received_date > box_sent_date))
> --This is the message I get
> Server: Msg 8141, Level 16, State 1, Line 1
> Column CHECK constraint for column 'Box_received_date' references
> another column, table 'box'.
> --This works when creating the table
> constraint ck_received_date
> check (box_received_date > ('box.box_sent_date')))
> --but if I try to insert data I get the following message
> insert box values(1, 9/9/1999,9/19/1999)
> Syntax error converting character string to smalldatetime data type.
> --What am I doing wrong - Thanks in advance
> Sent via Deja.com http://www.deja.com/
> Before you buy.