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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

And it will show you your exact rule definition:<br />

Text<br />

------------------------------------<br />

CREATE RULE SalaryRule<br />

AS @Salary > 0<br />

Now we’ve got a rule, but it isn’t doing anything. If we tried to insert a record in our Employees table,<br />

we could still insert any value right now without any restrictions beyond data type.<br />

To activate the rule, we need to make use of a special stored procedure called sp_bindrule. We want to<br />

bind our SalaryRule to the Salary column of our Employees table. The syntax looks like this:<br />

sp_bindrule , , []<br />

The rule part is simple enough; that’s the rule we want to bind. The object_name is also simple; it’s<br />

the object (column or user-defined data type) to which we want to bind the rule. The only odd parameter<br />

is the futureonly_flag, and it applies only when the rule is bound to a user-defined data type. The<br />

default is for this to be off. However, if you set it to True or pass in a 1, then the binding of the rule will<br />

apply only to new columns to which you bind the user-defined data type. Any columns that already<br />

have the data type in its old form will continue to use that form.<br />

Since we’re just binding this rule to a column, our syntax requires only the first two parameters:<br />

EXEC sp_bindrule ‘SalaryRule’, ‘Employees.Salary’;<br />

Take a close look at the object_name parameter. We have Employees and Salary separated by a “.”<br />

Why is that? Since the rule isn’t associated with any particular table until you bind it, you need to state<br />

the table and column to which the rule will be bound. If you do not use the tablename.column naming<br />

structure, then <strong>SQL</strong> <strong>Server</strong> will assume that what you’re naming must be a user-defined data type. If it<br />

doesn’t find one, you’ll get back an error message that can be a bit confusing if you hadn’t intended to<br />

bind the rule to a data type:<br />

Msg 15148, Level 16, State 1, Procedure sp_bindrule, Line 190<br />

The data type or table column ‘Salary’ does not exist or you do not have<br />

permission.<br />

In our case, trying to insert or update an Employees record with a negative value violates the rule and<br />

generates an error.<br />

If we want to remove our rule from use with this column, we make use of sp_unbindrule:<br />

EXEC sp_unbindrule ‘Employees.Salary’;<br />

Chapter 6: Constraints<br />

The futureonly_flag parameter is usually an option, but doesn’t apply to this particular example. If<br />

you use sp_unbindrule with the futureonly_flag turned on, and it is used against a user-defined<br />

data type (rather than a specific column), then the unbinding will only apply to future uses of that data<br />

type — existing columns using that data type will still make use of the rule.<br />

181

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

Saved successfully!

Ooh no, something went wrong!