トリガーの作成、変更、および削除

SMO では、Trigger オブジェクトを使用してトリガーが表現されます。トリガーの起動時に実行される Transact-SQL コードは、Trigger オブジェクトの TextBody プロパティによって設定します。トリガーの型は、Update プロパティなど、Trigger オブジェクトのその他のプロパティを使用することで設定します。これは、親テーブル上のレコードの UPDATE によってトリガーが起動されるかどうかを指定する、ブール型のプロパティです。

Trigger オブジェクトは、従来のデータ操作言語 (DML) トリガーを表します。SQL Server 2008 では、データ定義言語 (DDL) トリガーもサポートされます。DDL トリガーは、DatabaseDdlTrigger オブジェクトおよび ServerDdlTrigger オブジェクトで表現します。

提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。詳細については、SQL Server オンライン ブックの「Visual Studio .NET で Visual Basic SMO プロジェクトを作成する方法」および「Visual Studio .NET で Visual C# SMO プロジェクトを作成する方法」を参照してください。

Visual Basic でのトリガーの作成、変更、および削除

このコード例では、AdventureWorks2008R2 データベースにある Sales という名前の既存のテーブル上に、更新トリガーを作成および挿入する方法を示します。トリガーによって、テーブルの更新時、または新規レコードの挿入時に通知メッセージが送信されます。

'Connect to the local, default instance of SQL Server.
Dim mysrv As Server
mysrv = New Server
'Reference the AdventureWorks2008R2 database.
Dim mydb As Database
mydb = mysrv.Databases("AdventureWorks2008R2")
'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# でのトリガーの作成、変更、および削除

このコード例では、AdventureWorks2008R2 データベースにある Sales という名前の既存のテーブル上に、更新トリガーを作成および挿入する方法を示します。トリガーによって、テーブルの更新時、または新規レコードの挿入時に通知メッセージが送信されます。

{
            //Connect to the local, default instance of SQL Server. 
            Server mysrv;
            mysrv = new Server();
            //Reference the AdventureWorks2008R2 database. 
            Database mydb;
            mydb = mysrv.Databases["AdventureWorks2008R2"];
            //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 でのトリガーの作成、変更、および削除

このコード例では、AdventureWorks2008R2 データベースにある Sales という名前の既存のテーブル上に、更新トリガーを作成および挿入する方法を示します。トリガーによって、テーブルの更新時、または新規レコードの挿入時に通知メッセージが送信されます。

# Set the path context to the local, default instance of SQL Server and to the
#database tables in Adventureworks2008R2
CD \sql\localhost\default\databases\AdventureWorks2008R2\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()