Tuesday, November 11, 2008

CREATE RULE (SQL 2005)

Creates an object called a rule. When bound to a column or an alias data type, a rule specifies the acceptable values that can be inserted into that column.

1-Creating a rule with a range

The following example creates a rule that restricts the range of integers inserted into the column or columns to which this rule is bound.

CREATE RULE range_rule
AS
@range>= $1000 AND @range <$20000;

----------------------------------------------

2- Creating a rule with a list

The following example creates a rule that restricts the actual values entered into the column or columns (to which this rule is bound) to only those listed in the rule.


CREATE RULE list_rule
AS
@list IN ('1389', '0736', '0877');

----------------------------------------------

3- Creating a rule with a pattern

The following example creates a rule to follow a pattern of any two characters followed by a hyphen (-), any number of characters or no characters, and ending with an integer from 0 through 9.


CREATE RULE pattern_rule
AS
@value LIKE '__-%[0-9]'

No comments: