17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

FedIDNo,<br />

DateInSystem)<br />

VALUES<br />

(‘MyCust’,<br />

‘123 Anywhere’,<br />

‘’,<br />

‘Reno’,<br />

‘NV’,<br />

80808,<br />

‘Joe Bob’,<br />

‘555-1212’,<br />

‘931234567’,<br />

GETDATE());<br />

Once again, we are able to INSERT non-conforming data to the table.<br />

By now, you may be asking how you know whether you have the constraint turned on or not. It would<br />

be pretty tedious if you had to create a bogus record to try to insert in order to test whether your constraint<br />

is active or not. Like most (but not all) of these kinds of dilemmas, <strong>SQL</strong> <strong>Server</strong> provides a procedure<br />

to indicate the status of a constraint, and it’s a procedure we’ve already seen,<br />

sp_helpconstraint. To execute it against our Customers table is easy:<br />

EXEC sp_helpconstraint Customers;<br />

The results are a little too verbose to fit into the pages of this book, but the second result set this procedure<br />

generates includes a column called status_enabled. Whatever this column says the status is can<br />

be believed. In this case, it should currently be Disabled.<br />

When we are ready for the constraint to be active again, we simply turn it back on by issuing the same<br />

command with a CHECK in the place of the NOCHECK:<br />

ALTER TABLE Customers<br />

CHECK<br />

CONSTRAINT CN_CustomerPhoneNo;<br />

Chapter 6: Constraints<br />

If you run the INSERT statement to verify that the constraint is again functional, you will see a familiar<br />

error message:<br />

Msg 547, Level 16, State 0, Line 1<br />

The INSERT statement conflicted with the CHECK constraint<br />

“CN_CustomerDateInSystem”. The conflict occurred in database “Accounting”, table<br />

“dbo.Customers”, column ‘DateInSystem’.<br />

The statement has been terminated.<br />

Our other option, of course, is to run sp_helpconstraint again, and check out the status_enabled<br />

column. If it shows as Enabled, then our constraint must be functional again.<br />

179

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!