Sql Server 2005 ensure columns sum within a table

5 pts.
Tags:
SQL
SQL Server 2005
How can I ensure the total sum of 5 columns within the same table equals 100 during insert or update in SQL Server 2005?

Software/Hardware used:
SQL Server 2005

Answer Wiki

Thanks. We'll let you know when a new response is added.

If all records already in the table comply with that restriction, one option would be adding a CHECK constraint to the table.

Something like this:

ALTER TABLE yourTable ADD CONSTRAINT ck_yourConstraint
CHECK (col1+col2+col3+col4+col5=100);

If NULLs are allowed on those columns, you might want to do something like this:

ALTER TABLE yourTable ADD CONSTRAINT ck_yourConstraint
CHECK (ISNULL(col1,0)+ISNULL(col2,0)+ISNULL(col3,0)+ISNULL(col4,0)+ISNULL(col5,0)=100);

-CarlosDL

———————-

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following