Friday 24 June 2016

Create a Rule in SQL SERVER

In This post we are going to see how to create a Rule in SQL SERVER


Format:

       CREATE RULE [ schema_name . ] 
       rule_name   
       AS condition_expression  [ ; ]  



Example:

CREATE TABLE countries(NAME VARCHAR(3),code INT)
go

CREATE RULE countryrule AS @con IN ('US','IND')
go
EXEC sys.sp_bindrule 'countryrule'-- nvarchar(776)
   'dbo.countries.name' -- nvarchar(776)
   
go



INSERT INTO countries(name ,code)
VALUES ('Ind',545)
INSERT INTO countries(name ,code)
VALUES ('US',545)
INSERT INTO countries(name ,code)
VALUES ('UK',545)

EXEC sys.sp_unbindrule 'countryrule'

Note : This feature will be removed from the microsoft in the future release,so avoid this feature in the development work, Modify the feature
with check constraint.




Example
ALTER TABLE countries
ADD CONSTRAINT countryrule CHECK(name IN ('IND','US','UK'))



From this post i hope you can learn how to create a Rule in SQL SERVER






No comments:

Post a Comment