Dela via


Rules

Rules are a backward-compatibility feature that perform some of the same functions as CHECK constraints. Using CHECK constraints is the preferred, standard way to restrict the values in a column. CHECK constraints are also more concise than rules. There can be only one rule applied to a column, but multiple CHECK constraints can be applied. CHECK constraints are specified as part of the CREATE TABLE statement, while rules are created as separate objects and then bound to the column.

Important

This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use CHECK constraints instead. For more information, see Constraints.

The following example creates a rule that specifies only numbers within a specified range. The rule is bound to a column of a table.

CREATE RULE id_chk AS @id BETWEEN 0 and 10000;
GO
CREATE TABLE cust_sample
   (
   cust_id            int
   PRIMARY KEY,
   cust_name         char(50),
   cust_address         char(50),
   cust_credit_limit   money,
   );
GO
sp_bindrule id_chk, 'cust_sample.cust_id';
GO