建立、改變和移除觸發程式

在 SMO 中,觸發程序是利用 Trigger 物件表示。在觸發程序引發時所執行的 Transact-SQL 程式碼是由 Trigger 物件的 TextBody 屬性所設定。觸發程序的類型是利用 Trigger 物件的其他屬性所設定,例如 Update 屬性。這是布林值屬性,指定觸發程序是否由記錄的 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()