Call now: (800) 766-1884  


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA

 SQL Server Scripts
 Scripts Menu




  SQL Server Tips by Gama and Naughter



Rules are constraints bound to columns from tables or to user defined data types. They force the data being inserted or updated to follow certain conditions. Rules contain one line of TSQL, with a conditional expression, that will enforce the constraint. They should be simple because their code is executed once for each row that is inserted or updated.

Rules are a backward compatibility feature and check constraints are preferable.

First the CREATE RULE statement will create the new rule, which needs to be bound to a column with a system stored procedure named sp_bindrule. Other useful sp's are: sp_help to get info on a rule, sp_helptext to see the text of a rule and sp_unbindrule to unbind the rule, before dropping it.

A virtual variable that can take any name will represent the new/changed data and it should be part of the conditional expression.


Example: add a rule to the phone column from table authors from the Pubs database. The rule will allow only data in the form 123 456-7890 or UNKNOWN.


This is the code for the rule:


CREATE RULE phone_rule


(@phone='UNKNOWN') OR (LEN(@phone)=12 AND


AND SUBSTRING(@phone,4,1)=' '


AND SUBSTRING(@phone,8,1)='-'

AND ISNUMERIC(RIGHT(@phone,4))=1 )


Binding the rule to the column:


EXEC sp_bindrule 'phone_rule', ''


Any attempt to use a different separator for the state or area code, or using a character other than a number will result in an error message.


Unbinding the rule (before dropping):


EXEC sp_unbindrule ''


Dropping the rule:


DROP RULE phone_rule

The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter


Burleson Consulting Remote DB Administration







Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.

Burleson Consulting
SQL Server database support


Copyright 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter