Creating a CLR Trigger : CLR « CLR « SQL Server / T-SQL Tutorial






CREATE TABLE dbo.MyAudit
(MyAuditID int
NOT NULL IDENTITY(1,1) PRIMARY KEY ,
LoadDT datetime NOT NULL)

CREATE ASSEMBLY YourTrigger
FROM 'C:\YourTrigger.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS
GO

CREATE TRIGGER dbo.trg_i_DWNotify
ON dbo.MyAudit AFTER INSERT
AS
EXTERNAL NAME
YourTrigger.[YourTrigger.DW_Trigger].ExportFile

INSERT dbo.MyAudit
(LoadDT)
VALUES(GETDATE())

///////////
Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlTypes
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Server
Imports System.IO

Public Class DW_Trigger
    Public Shared Sub ExportFile()
        Dim DWTrigger As SqlTriggerContext
        DWTrigger = SqlContext.TriggerContext

        If (DWTrigger.TriggerAction = TriggerAction.Insert) Then
            Dim DWsw As StreamWriter = New StreamWriter("C:\test.txt")
            DWsw.WriteLine(Now())
            DWsw.Close()
        End If
    End Sub
End Class








29.2.CLR
29.2.1.Converting SQL Server to CLR Data Types
29.2.2.--Enabling CLR Support in SQL Server 2005
29.2.3.Loading the Assembly Into SQL Server
29.2.4.Creating the CLR Stored Procedure
29.2.5.Creating a CLR Scalar User-Defined Function
29.2.6.Creating a CLR Trigger