mysql2005触发器修改成绩_创建、更改和删除触发器

本文介绍了如何在SQL Server中使用SMO库创建、更改和删除触发器,特别是在AdventureWorks2012数据库的Sales表上创建更新和插入触发器,当表被更新或插入新记录时,触发器会发送提醒消息。
摘要由CSDN通过智能技术生成

创建、更改和删除触发器Creating, Altering, and Removing Triggers

08/06/2017

本文内容

适用于:Applies to: 719f28649793c602f9270966b5ed5c39.pngSQL ServerSQL Server(所有支持的版本)719f28649793c602f9270966b5ed5c39.pngSQL ServerSQL Server (all supported versions) 719f28649793c602f9270966b5ed5c39.pngAzure SQL 数据库Azure SQL Database719f28649793c602f9270966b5ed5c39.pngAzure SQL 数据库Azure SQL Database 719f28649793c602f9270966b5ed5c39.pngAzure SQL 托管实例Azure SQL Managed Instance719f28649793c602f9270966b5ed5c39.pngAzure SQL 托管实例Azure SQL Managed Instance 719f28649793c602f9270966b5ed5c39.pngAzure Synapse AnalyticsAzure Synapse Analytics719f28649793c602f9270966b5ed5c39.pngAzure Synapse AnalyticsAzure Synapse Analytics适用于:Applies to: 719f28649793c602f9270966b5ed5c39.pngSQL ServerSQL Server(所有支持的版本)719f28649793c602f9270966b5ed5c39.pngSQL ServerSQL Server (all supported versions) 719f28649793c602f9270966b5ed5c39.pngAzure SQL 数据库Azure SQL Database719f28649793c602f9270966b5ed5c39.pngAzure SQL 数据库Azure SQL Database 719f28649793c602f9270966b5ed5c39.pngAzure SQL 托管实例Azure SQL Managed Instance719f28649793c602f9270966b5ed5c39.pngAzure SQL 托管实例Azure SQL Managed Instance 719f28649793c602f9270966b5ed5c39.pngAzure Synapse AnalyticsAzure Synapse Analytics719f28649793c602f9270966b5ed5c39.pngAzure 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值