I'm creating a relatively simple SQL CLR in C#.
The job of this CLR is to publish events to an Azure Event Grid Topic.
I would like to be able to call this particular CLR from any number of triggers for any number of tables (in actuality there will be few).
Given the core code below how does one accurately retrieve the name of the table that caused/fired the trigger? I have little clue where to start.
I would like to be able to remove the SqlTrigger
attribute and somehow access the table name for the var table
property.
// I Would like to leave the attribute commented out
//
//[Microsoft.SqlServer.Server.SqlTrigger(Name = "PublishToEventGridTrigger",
// Target = "NamesOfThings", Event = "FOR UPDATE, INSERT, DELETE")]
public static void PublishToEventGridTrigger()
{
// TODO - How should these settings be handled?
// Not sure if config files are accessible... To test..
// ***************************************************************
string topicHost = "https://####.eventgrid.azure.net/api/events";
string topicKey = "####";
// TODO - Get Table name for this
var table = "How Do I set This";
string eventType = $"##.{table}.{SqlContext.TriggerContext.TriggerAction.ToString()}";
// ***************************************************************
try
{
// extract data involved in trigger
// Create the Event object
EventGridEvent evt = new EventGridEvent("QWDBEvent", eventType, new
TriggerData(SqlContext.TriggerContext, SqlContext.Pipe));
// Publish the event
Event.Publish(topicHost, topicKey, evt);
}
catch (Exception ex)
{
//TODO - how do we handle these through SQL CLR?
// Going with a fire-and-forget for now
SqlContext.Pipe.Send($"Failure firing {ex.Message}");
}
SqlContext.Pipe.Send($"Trigger fired");
}