sql server 2005 :how to put not null constraint on a column depending upon value in other column?

StackOverflow https://stackoverflow.com/questions/823481

  •  05-07-2019
  •  | 
  •  

Question

in a table two of columns are billable(bit),billabledate(datetime).i want billable date to be not null if billable is not null.

Was it helpful?

Solution

Add a check constraint:

CHECK (billable is not null and billabledate is not null) OR (billable is null)

OTHER TIPS

You need a Check Constraint

ALTER TABLE dbo.Table WITH NOCHECK
ADD CONSTRAINT CK_Table_BusinessRule CHECK (Billable IS NOT NULL AND BillableDate IS NOT NULL)

http://msdn.microsoft.com/en-us/library/ms179491(SQL.90).aspx

I'd try adding a trigger to the table, on after insert and after update, to enforce that constraint. Check the billable value, and block insert/update in case it is not null and the billabledate is null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top