fixed and expanded code formatting; minor grammar tweak
Source Link

You'll probably need to do three tests within the constraint. One, one test for each pair that you want to be null and one for the column that should be not null.:

ALTER TABLE table
ADD CONSTRAINT CK_one_is_null
CHECK (Col1 
 is not null and (col1 IS NOT NULL AND col2 isIS nullNULL andAND col3 isIS nullNULL) 
 or OR (col2 isIS notNOT nullNULL andAND col1 isIS nullNULL andAND col3 isIS nullNULL) or
  OR (col3 isIS notNOT nullNULL andAND col1 isIS nullNULL andAND col2 isIS nullNULL)
);

My syntax will need fixing, but you should get the idea.

You'll probably need to do three tests within the constraint. One test for each pair that you want to be null and for the column that should be not null.

(Col1 is not null and col2 is null and col3 is null) or (col2 is not null and col1 is null and col3 is null) or (col3 is not null and col1 is null and col2 is null)

My syntax will need fixing, but you should get the idea.

You'll probably need to do three tests within the constraint, one test for each pair that you want to be null and one for the column that should be not null:

ALTER TABLE table
ADD CONSTRAINT CK_one_is_null
CHECK ( 
     (col1 IS NOT NULL AND col2 IS NULL AND col3 IS NULL) 
  OR (col2 IS NOT NULL AND col1 IS NULL AND col3 IS NULL) 
  OR (col3 IS NOT NULL AND col1 IS NULL AND col2 IS NULL)
);
Source Link
mrdenny
  • 26.7k
  • 2
  • 40
  • 79

You'll probably need to do three tests within the constraint. One test for each pair that you want to be null and for the column that should be not null.

(Col1 is not null and col2 is null and col3 is null) or (col2 is not null and col1 is null and col3 is null) or (col3 is not null and col1 is null and col2 is null)

My syntax will need fixing, but you should get the idea.