Given a table tblABC, I want to ensure that if a value is entered in field fld1 it is between 60 and 240 and that a value between 0 and 100 is entered in field fld2, or no value is entered for either field fld1 or field fld2. In other words both or neither.
I have tried this:
ALTER TABLE tblABC
ADD CONSTRAINT CK1tblABC CHECK (((fld1 IS NULL) AND (fld2 IS NULL)) OR ((fld1 between 60 AND 240) AND (fld2 between 0 AND 100)))
and this:
ALTER TABLE tblABC
ADD CONSTRAINT CK1tblABC CHECK (((fld1 IS NULL) AND (fld2 IS NULL)) OR ((fld1 >= 60 AND fld1 <= 240) AND (fld2 >= 0 AND fld2 <= 100)))
but neither constraint works. Both allow a value between 60 and 240 in fld1 and a null value in fld2. In both cases SQL Server rearranges the bracketing.
Any ideas as to how to phrase the TSQL for the constraint?
I have tried this:
ALTER TABLE tblABC
ADD CONSTRAINT CK1tblABC CHECK (((fld1 IS NULL) AND (fld2 IS NULL)) OR ((fld1 between 60 AND 240) AND (fld2 between 0 AND 100)))
and this:
ALTER TABLE tblABC
ADD CONSTRAINT CK1tblABC CHECK (((fld1 IS NULL) AND (fld2 IS NULL)) OR ((fld1 >= 60 AND fld1 <= 240) AND (fld2 >= 0 AND fld2 <= 100)))
but neither constraint works. Both allow a value between 60 and 240 in fld1 and a null value in fld2. In both cases SQL Server rearranges the bracketing.
Any ideas as to how to phrase the TSQL for the constraint?