--- title: "Creating, Altering, and Removing Rules | Microsoft Docs" ms.custom: "" ms.date: "06/13/2017" ms.prod: "sql-server-2014" ms.reviewer: "" ms.technology: ms.topic: "reference" helpviewer_keywords: - "rules [SMO]" ms.assetid: 16981459-524e-4b39-a899-4370eaf763cc author: stevestein ms.author: sstein manager: craigg --- # Creating, Altering, and Removing Rules In SMO, rules are represented by the object. The rule is defined by the property, which is a text string that contains a condition expression that uses operators or predicates, such as IN, LIKE, or BETWEEN. A rule cannot reference columns or other database objects. Built-in functions that do not reference database objects can be included. The definition in the property must contain a variable that refers to the data value entered. Any name or symbol can be used to represent the value when creating the rule, but the first character must be the \@ symbol. ## Example To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see [Create a Visual Basic SMO Project in Visual Studio .NET](../../../database-engine/dev-guide/create-a-visual-basic-smo-project-in-visual-studio-net.md) or [Create a Visual C# SMO Project in Visual Studio .NET](../how-to-create-a-visual-csharp-smo-project-in-visual-studio-net.md). ## Creating, Altering, and Removing a Rule in Visual Basic This code sample shows how to create a rule, attach it to a column, modify properties of the object, detach it from the column, and then drop it. The `Dim` statement for the object is specified with the full assembly path to avoid ambiguity with a object in the System.Data assembly. ## Creating, Altering, and Removing a Rule in Visual C# This code sample shows how to create a rule, attach it to a column, modify properties of the object, detach it from the column, and then drop it. The `Dim` statement for the object is specified with the full assembly path to avoid ambiguity with a object in the System.Data assembly. ```csharp { //Connect to the local, default instance of SQL Server. Server srv; srv = new Server(); //Reference the AdventureWorks2012 database. Database db; db = srv.Databases["AdventureWorks2012"]; //Define a Rule object variable by supplying the parent database, name and schema in the constructor. //Note that the full namespace must be given for the Rule type to differentiate it from other Rule types. Microsoft.SqlServer.Management.Smo.Rule ru; ru = new Rule(db, "TestRule", "Production"); //Set the TextHeader and TextBody properties to define the rule. ru.TextHeader = "CREATE RULE [Production].[TestRule] AS"; ru.TextBody = "@value BETWEEN GETDATE() AND DATEADD(year,4,GETDATE())"; //Create the rule on the instance of SQL Server. ru.Create(); //Bind the rule to a column in the Product table by supplying the table, schema, and //column as arguments in the BindToColumn method. ru.BindToColumn("Product", "SellEndDate", "Production"); //Unbind from the column before removing the rule from the database. ru.UnbindFromColumn("Product", "SellEndDate", "Production"); ru.Drop(); } ``` ## Creating, Altering, and Removing a Rule in PowerShell This code sample shows how to create a rule, attach it to a column, modify properties of the object, detach it from the column, and then drop it. The `Dim` statement for the object is specified with the full assembly path to avoid ambiguity with a object in the System.Data assembly. ```powershell # Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2012 CD \sql\localhost\default\databases $db = Get-Item Adventureworks2012 # Define a Rule object variable by supplying the parent database, name and schema in the constructor. $ru = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Rule -argumentlist $db, "TestRule", "Production" #Set the TextHeader and TextBody properties to define the rule. $ru.TextHeader = "CREATE RULE [Production].[TestRule] AS" $ru.TextBody = "@value BETWEEN GETDATE() AND DATEADD(year,4,GETDATE())" #Create the rule on the instance of SQL Server. $ru.Create() # Bind the rule to a column in the Product table by supplying the table, schema, and column as arguments in the BindToColumn method. $ru.BindToColumn("Product", "SellEndDate", "Production") #Unbind from the column before removing the rule from the database. $ru.UnbindFromColumn("Product", "SellEndDate", "Production") $ru.Drop() ``` ## See Also