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.
| Author |
Topic |
|
II Traveler II
Starting Member
11 Posts |
Posted - 2011-11-22 : 12:46:02
|
So I have coded an Alter Table statement that adds a constraint that requires the InvoiceTotal to be greater than the sum of the PaymentTotal and the CreditTotal. The problem is, when I run the statement, I get the error "The ALTER TABLE statement conflicted with the CHECK constraint "Constraint2". The conflict occurred in database "AP", table "dbo.Invoice"." Any solutions? Here's the code.ALTER TABLE InvoicesADD CONSTRAINT Constraint2 CHECK (InvoiceTotal > (PaymentTotal + CreditTotal)) |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-11-22 : 12:53:59
|
It appears you have rows that violate that constraint. Do a select from your table using the check constraint as your predicate and see which rows, currently, violate that constraint. I.E.: SELECT *FROM InvoicesWHERE NOT(InvoiceTotal > (PaymentTotal + CreditTotal)) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-22 : 12:56:58
|
you can create it like below if you want it to ignore existing values (though it doesnt make much sense to retain them like that!)ALTER TABLE Invoices WITH NOCHECKADD CONSTRAINT Constraint2 CHECK (InvoiceTotal > (PaymentTotal + CreditTotal)) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
II Traveler II
Starting Member
11 Posts |
Posted - 2011-11-23 : 17:38:30
|
| Thanks Lamprey! I was afraid that my rows might have violated the rule. I just didn't think of a way to check or confirm that.And thanks to you, visakh16! You always come up with a great solution! I should have thought of that. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-25 : 08:42:20
|
quote: Originally posted by II Traveler II Thanks Lamprey! I was afraid that my rows might have violated the rule. I just didn't think of a way to check or confirm that.And thanks to you, visakh16! You always come up with a great solution! I should have thought of that.
wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|