Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
SQL database in Microsoft Fabric
In SMO, rules are represented by the Rule object. The rule is defined by the TextBody 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 TextBody 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.
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 C# SMO Project in Visual Studio .NET.
This code sample shows how to create a rule, attach it to a column, modify properties of the Rule object, detach it from the column, and then drop it.
The Dim statement for the Rule object is specified with the full assembly path to avoid ambiguity with a Rule object in the System.Data assembly.
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
'Declare a Table object variable and reference the Product table.
Dim tb As Table
tb = db.Tables("Product", "Production")
'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.
Dim ru As Microsoft.SqlServer.Management.Smo.Rule
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()
This code sample shows how to create a rule, attach it to a column, modify properties of the Rule object, detach it from the column, and then drop it.
The Dim statement for the Rule object is specified with the full assembly path to avoid ambiguity with a Rule object in the System.Data assembly.
{
//Connect to the local, default instance of SQL Server.
Server srv;
srv = new Server();
//Reference the AdventureWorks2022 database.
Database db;
db = srv.Databases["AdventureWorks2022"];
//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();
}
This code sample shows how to create a rule, attach it to a column, modify properties of the Rule object, detach it from the column, and then drop it.
The Dim statement for the Rule object is specified with the full assembly path to avoid ambiguity with a Rule object in the System.Data assembly.
# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2022
CD \sql\localhost\default\databases
$db = get-item AdventureWorks2022
# 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()
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register today