규칙 만들기, 변경 및 제거

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse Analytics

SMO에서 규칙은 개체로 Rule 표시됩니다. 규칙은 IN, LIKE 또는 BETWEEN과 같은 연산자 또는 조건자를 사용하는 조건식을 포함하는 텍스트 문자열인 속성에 의해 TextBody 정의됩니다. 규칙은 열 또는 기타 데이터베이스 개체를 참조할 수 없습니다. 데이터베이스 개체를 참조하지 않는 기본 제공 함수는 포함할 수 있습니다.

TextBody 속성 정의에는 입력한 데이터 값을 참조하는 변수가 포함되어야 합니다. 모든 이름이나 기호를 사용하여 규칙을 만들 때 값을 나타낼 수 있지만 첫 번째 문자는 @ 기호여야 합니다.

제공된 코드 예제를 사용하려면 프로그래밍 환경, 프로그래밍 템플릿 및 애플리케이션을 만들 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기를 참조하세요.

Visual Basic에서 규칙 만들기, 변경 및 제거

이 코드 샘플에서는 규칙을 만들고, 열에 연결하고, 개체의 Rule 속성을 수정하고, 열에서 분리한 다음 삭제하는 방법을 보여줍니다.

개체의 Rule Dim 문은 System.Data 어셈블리의 개체와의 모호성을 방지하기 위해 전체 어셈블리 경로로 Rule 지정됩니다.

'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()

Visual C에서 규칙 만들기, 변경 및 제거#

이 코드 샘플에서는 규칙을 만들고, 열에 연결하고, 개체의 Rule 속성을 수정하고, 열에서 분리한 다음 삭제하는 방법을 보여줍니다.

개체의 Rule Dim 문은 System.Data 어셈블리의 개체와의 모호성을 방지하기 위해 전체 어셈블리 경로로 Rule 지정됩니다.

{  
            //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();  
  
        }  

PowerShell에서 규칙 만들기, 변경 및 제거

이 코드 샘플에서는 규칙을 만들고, 열에 연결하고, 개체의 Rule 속성을 수정하고, 열에서 분리한 다음 삭제하는 방법을 보여줍니다.

개체의 Rule Dim 문은 System.Data 어셈블리의 개체와의 모호성을 방지하기 위해 전체 어셈블리 경로로 Rule 지정됩니다.

# 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()  

참고 항목

Rule