--- title: "Creating, Altering, and Removing Triggers | Microsoft Docs" ms.custom: "" ms.date: "03/06/2017" ms.prod: "sql-server-2014" ms.reviewer: "" ms.technology: ms.topic: "reference" helpviewer_keywords: - "triggers [SMO]" ms.assetid: 8ddbe23b-6e31-4f8e-8a70-17bd5072413e author: stevestein ms.author: sstein manager: craigg --- # Creating, Altering, and Removing Triggers In SMO, triggers are represented by using the object. The [!INCLUDE[tsql](../../../includes/tsql-md.md)] code that runs when the trigger that is fired is set by the property of the Trigger object. The type of trigger is set by using other properties of the object, such as the property. This is a Boolean property that specifies whether the trigger is fired by an `UPDATE` of records on the parent table. The object represents traditional, data manipulation language (DML) triggers. In [!INCLUDE[ssKatmai](../../../includes/sskatmai-md.md)] and later versions, data definition language (DDL) triggers are also supported. DDL triggers are represented by the object and the object. ## Example [!INCLUDE[ssChooseProgEnv](../../../includes/sschooseprogenv-md.md)] ## Creating, Altering, and Removing a Trigger in Visual Basic This code example shows how to create and insert an update trigger on an existing table, named `Sales`, in the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal-md.md)] database. The trigger sends a reminder message when the table is updated or a new record is inserted. ## Creating, Altering, and Removing a Trigger in Visual C# This code example shows how to create and insert an update trigger on an existing table, named `Sales`, in the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal-md.md)] database. The trigger sends a reminder message when the table is updated or a new record is inserted. ```csharp { //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(); } ``` ## Creating, Altering, and Removing a Trigger in PowerShell This code example shows how to create and insert an update trigger on an existing table, named `Sales`, in the [!INCLUDE[ssSampleDBnormal](../../../includes/sssampledbnormal-md.md)] database. The trigger sends a reminder message when the table is updated or a new record is inserted. ```powershell # 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() ```