トリガーの作成、変更、および削除
適用対象: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics
SMO では、Trigger オブジェクトを使用してトリガーが表現されます。 トリガーが発生したときに実行される Transact-SQL コードは、Trigger オブジェクトの TextBody プロパティによって設定されます。 トリガーの型は、Trigger プロパティなど、Update オブジェクトのその他のプロパティを使用することで設定します。 これは、親テーブルのレコードの UPDATE によってトリガーが起動されるかどうかを指定するブール型プロパティです。
Trigger オブジェクトは、従来のデータ操作言語 (DML) トリガーを表します。 SQL Server 2008 (10.0.x) 以降のバージョンでは、データ定義言語 (DDL) トリガーもサポートされています。 DDL トリガーは、DatabaseDdlTrigger オブジェクトおよび ServerDdlTrigger オブジェクトで表現します。
例
提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。 詳細については、「 Visual Studio .NET で Visual C# SMO プロジェクトを作成するを参照してください。
Visual Basic でのトリガーの作成、変更、および削除
このコード例では、AdventureWorks2022 データベース内の Sales
という名前の既存のテーブルに更新トリガーを作成して挿入する方法を示します。 トリガーによって、テーブルの更新時、または新規レコードの挿入時に通知メッセージが送信されます。
'Connect to the local, default instance of SQL Server.
Dim mysrv As Server
mysrv = New Server
'Reference the AdventureWorks2022 database.
Dim mydb As Database
mydb = mysrv.Databases("AdventureWorks2022")
'Declare a Table object variable and reference the Customer table.
Dim mytab As Table
mytab = mydb.Tables("Customer", "Sales")
'Define a Trigger object variable by supplying the parent table, schema ,and name in the constructor.
Dim tr As Trigger
tr = New Trigger(mytab, "Sales")
'Set TextMode property to False, then set other properties to define the trigger.
tr.TextMode = False
tr.Insert = True
tr.Update = True
tr.InsertOrder = Agent.ActivationOrder.First
Dim stmt As String
stmt = " RAISERROR('Notify Customer Relations',16,10) "
tr.TextBody = stmt
tr.ImplementationType = ImplementationType.TransactSql
'Create the trigger on the instance of SQL Server.
tr.Create()
'Remove the trigger.
tr.Drop()
Visual C# でのトリガーの作成、変更、および削除
このコード例では、AdventureWorks2022 データベース内の Sales
という名前の既存のテーブルに更新トリガーを作成して挿入する方法を示します。 トリガーによって、テーブルの更新時、または新規レコードの挿入時に通知メッセージが送信されます。
{
//Connect to the local, default instance of SQL Server.
Server mysrv;
mysrv = new Server();
//Reference the AdventureWorks2022 database.
Database mydb;
mydb = mysrv.Databases["AdventureWorks2022"];
//Declare a Table object variable and reference the Customer table.
Table mytab;
mytab = mydb.Tables["Customer", "Sales"];
//Define a Trigger object variable by supplying the parent table, schema ,and name in the constructor.
Trigger tr;
tr = new Trigger(mytab, "Sales");
//Set TextMode property to False, then set other properties to define the trigger.
tr.TextMode = false;
tr.Insert = true;
tr.Update = true;
tr.InsertOrder = ActivationOrder.First;
string stmt;
stmt = " RAISERROR('Notify Customer Relations',16,10) ";
tr.TextBody = stmt;
tr.ImplementationType = ImplementationType.TransactSql;
//Create the trigger on the instance of SQL Server.
tr.Create();
//Remove the trigger.
tr.Drop();
}
PowerShell でのトリガーの作成、変更、および削除
このコード例では、AdventureWorks2022 データベース内の Sales
という名前の既存のテーブルに更新トリガーを作成して挿入する方法を示します。 トリガーによって、テーブルの更新時、または新規レコードの挿入時に通知メッセージが送信されます。
# Set the path context to the local, default instance of SQL Server and to the
#database tables in AdventureWorks2022
CD \sql\localhost\default\databases\AdventureWorks2022\Tables\
#Get reference to the trigger's target table
$mytab = get-item Sales.Customer
# Define a Trigger object variable by supplying the parent table, schema ,and name in the constructor.
$tr = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Trigger `
-argumentlist $mytab, "Sales"
# Set TextMode property to False, then set other properties to define the trigger.
$tr.TextMode = $false
$tr.Insert = $true
$tr.Update = $true
$tr.InsertOrder = [Microsoft.SqlServer.Management.SMO.Agent.ActivationOrder]::First
$tr.TextBody = " RAISERROR('Notify Customer Relations',16,10) "
$tr.ImplementationType = [Microsoft.SqlServer.Management.SMO.ImplementationType]::TransactSql
# Create the trigger on the instance of SQL Server.
$tr.Create()
#Remove the trigger.
$tr.Drop()