Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Conditional Constraint?!

Author  Topic 

loubeelou
Starting Member

4 Posts

Posted - 2011-11-28 : 17:31:40
One of my tables requires quite an unusual constraint and I'm not really sure how to go about implementing it. Basically, if a certain column has a specific value (e.g. 'I'), certain columns need to be filled in and others need to be set as NULL. There are 2 possible values in the column that determines which columns should be NULL and not.

I assume I'm doing it very very wrong, but I tried to do a conditional constraint (I put "CHECK(name IS NULL IF type='I')") There must be an easier way to do this! :-( Any ideas/hints?

Cheers :-)

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-11-28 : 18:03:12
CHECK((name IS NULL AND type='I') OR (name IS NOT NULL AND type<>'I'))
Go to Top of Page

loubeelou
Starting Member

4 Posts

Posted - 2011-11-29 : 07:32:55
Brilliant, thanks so much! :-)

quote:
Originally posted by robvolk

CHECK((name IS NULL AND type='I') OR (name IS NOT NULL AND type<>'I'))

Go to Top of Page

loubeelou
Starting Member

4 Posts

Posted - 2011-11-29 : 07:48:42
I just tried this, and got the following error...



:-(
Go to Top of Page

loubeelou
Starting Member

4 Posts

Posted - 2011-11-29 : 10:12:32
I looked it up and it was just because I was trying to check in-line.

Thanks so much for your help!! :-)
Go to Top of Page
   

- Advertisement -