创建、更改和删除触发器Creating, Altering, and Removing Triggers
08/06/2017
本文内容
适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQL ServerSQL Server (all supported versions) Azure SQL 数据库Azure SQL DatabaseAzure SQL 数据库Azure SQL Database Azure SQL 托管实例Azure SQL Managed InstanceAzure SQL 托管实例Azure SQL Managed Instance Azure Synapse AnalyticsAzure Synapse AnalyticsAzure Synapse AnalyticsAzure Synapse Analytics适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQL ServerSQL Server (all supported versions) Azure SQL 数据库Azure SQL DatabaseAzure SQL 数据库Azure SQL Database Azure SQL 托管实例Azure SQL Managed InstanceAzure SQL 托管实例Azure SQL Managed Instance Azure Synapse AnalyticsAzure Synapse AnalyticsAzure Synapse AnalyticsAzure Synapse Analytics
在 SMO 中,触发器由 Trigger 对象表示。In SMO, triggers are represented by using the Trigger object. Transact-SQLTransact-SQL触发器对象的属性设置所激发的触发器时运行的代码 TextBody 。The Transact-SQLTransact-SQL code that runs when the trigger that is fired is set by the TextBody property of the Trigger object. 使用 Trigger 对象的其他属性(如 Update 属性)可以设置触发器的类型。The type of trigger is set by using other properties of the Trigger object, such as the Update property. 这是一个布尔属性,该属性指定是否由对父表上的记录的 更新 触发触发器。This is a Boolean property that specifies whether the trigger is fired by an UPDATE of records on the parent table.
Trigger 对象表示传统的数据操作语言 (DML) 触发器。The Trigger object represents traditional, data manipulation language (DML) triggers. 在 SQL Server 2008SQL Server 2008 和更改版本中,也同样支持数据定义语言 (DDL) 触发器。In SQL Server 2008SQL Server 2008 and later versions, data definition language (DDL) triggers are also supported. DDL triggers are represented by the DatabaseDdlTrigger object and the ServerDdlTrigger object.
示例Example
若要使用所提供的任何代码示例,您必须选择创建应用程序所需的编程环境、编程模板和编程语言。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.
在 Visual Basic 中创建、更改和删除触发器Creating, Altering, and Removing a Trigger in Visual Basic
此代码示例演示如何在 AdventureWorks2012AdventureWorks2012 数据库中名为 Sales 的现有表上创建并插入更新触发器。This code example shows how to create and insert an update trigger on an existing table, named Sales, in the AdventureWorks2012AdventureWorks2012 database. 当更新表或插入新记录时触发器会发送提醒消息。The trigger sends a reminder message when the table is updated or a new record is inserted.
'Connect to the local, default instance of SQL Server.
Dim mysrv As Server
mysrv = New Server
'Reference the AdventureWorks2012 2008R2 database.
Dim mydb As Database
mydb = mysrv.Databases("AdventureWorks2012")
'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# 中创建、更改和删除触发器Creating, Altering, and Removing a Trigger in Visual C#
此代码示例演示如何在 AdventureWorks2012AdventureWorks2012 数据库中名为 Sales 的现有表上创建并插入更新触发器。This code example shows how to create and insert an update trigger on an existing table, named Sales, in the AdventureWorks2012AdventureWorks2012 database. 当更新表或插入新记录时触发器会发送提醒消息。The trigger sends a reminder message when the table is updated or a new record is inserted.
{
//Connect to the local, default instance of SQL Server.
Server mysrv;
mysrv = new Server();
//Reference the AdventureWorks2012 database.
Database mydb;
mydb = mysrv.Databases["AdventureWorks2012"];
//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 中创建、更改和删除触发器Creating, Altering, and Removing a Trigger in PowerShell
此代码示例演示如何在 AdventureWorks2012AdventureWorks2012 数据库中名为 Sales 的现有表上创建并插入更新触发器。This code example shows how to create and insert an update trigger on an existing table, named Sales, in the AdventureWorks2012AdventureWorks2012 database. 当更新表或插入新记录时触发器会发送提醒消息。The trigger sends a reminder message when the table is updated or a new record is inserted.
# Set the path context to the local, default instance of SQL Server and to the
#database tables in Adventureworks2012
CD \sql\localhost\default\databases\AdventureWorks2012\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()