Condividi tramite


Creazione, modifica e rimozione di trigger

In SMO i trigger sono rappresentati tramite l'oggetto Trigger. Codice Transact-SQL che viene eseguito quando il trigger attivato viene impostato dalla proprietà TextBody dell'oggetto Trigger. Il tipo di trigger viene impostato tramite altre proprietà dell'oggetto Trigger, ad esempio la proprietà Update. Si tratta di una proprietà booleana che specifica se il trigger viene attivato da una parola chiave UPDATE di record nella tabella padre.

L'oggetto Trigger rappresenta trigger DML (Data Manipulation Language) tradizionali. In SQL Server 2008 sono supportati anche i trigger DDL (Data Definition Language). I trigger DDL sono rappresentati dall'oggetto DatabaseDdlTrigger e dall'oggetto ServerDdlTrigger.

Esempio

Per utilizzare qualsiasi esempio di codice fornito, è necessario scegliere l'ambiente di programmazione, il modello di programmazione e il linguaggio di programmazione per la creazione dell'applicazione. Per ulteriori informazioni, vedere "Procedura: Creare un progetto Visual Basic SMO in Visual Studio .NET" o "Procedura: Creare un progetto Visual C# SMO in Visual Studio .NET" nella documentazione online di SQL Server.

Creazione, modifica e rimozione di un trigger in Visual Basic

In questo esempio di codice viene illustrato come creare e inserire un trigger di aggiornamento in una tabella esistente, denominata Sales, nel database di AdventureWorks2008R2. Il trigger invia un messaggio di promemoria quando la tabella viene aggiornata o quando viene inserito un nuovo record.

'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()

Creazione, modifica e rimozione di un trigger in Visual C#

In questo esempio di codice viene illustrato come creare e inserire un trigger di aggiornamento in una tabella esistente, denominata Sales nel database di AdventureWorks2008R2. Il trigger invia un messaggio di promemoria quando la tabella viene aggiornata o quando viene inserito un nuovo record.

{
            //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();
        }

Creazione, modifica e rimozione di un trigger in PowerShell

In questo esempio di codice viene illustrato come creare e inserire un trigger di aggiornamento in una tabella esistente, denominata Sales, nel database di AdventureWorks2008R2. Il trigger invia un messaggio di promemoria quando la tabella viene aggiornata o quando viene inserito un nuovo record.

# 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()