Friday, February 24, 2012

Alter a column to allow null or not null values if meets criteria

I'm very new working with SQL server and I'm trying to create a check
constraint that will allow the an specific field to be null only if the
result on a second field is zero and not null if this result is greater
than zero.
My boss is pushing me to implement this criteria on my SQL database
right away please Help...For example:
ALTER TABLE your_table
ADD CONSTRAINT ck_constraint_name
CHECK ((col1 IS NULL AND col2 = 0)
OR (col1 IS NOT NULL AND col2 > 0)) ;
I hope your boss intends that you apply this to a TEST system and TEST
the impact rather than go right away into production...
David Portas
SQL Server MVP
--|||Thanks David, It works perfect you are the best...|||May be easier to write a trigger that examines the inserted table to check
for the values.
"imagabo" <imagabo@.hotmail.com> wrote in message
news:1130424448.351441.168020@.g47g2000cwa.googlegroups.com...
> I'm very new working with SQL server and I'm trying to create a check
> constraint that will allow the an specific field to be null only if the
> result on a second field is zero and not null if this result is greater
> than zero.
> My boss is pushing me to implement this criteria on my SQL database
> right away please Help...
>

No comments:

Post a Comment