我们有一个后台系统,可以通过以下方式将发票信息插入到 SQL 数据库中:MSDTC http://en.wikipedia.org/wiki/Distributed_Transaction_Coordinator存储过程中,应用程序插入标头,然后插入详细信息。
我已经设置了一个CLR http://msdn.microsoft.com/en-us/library/ms254498(v=vs.80).aspx插入记录时触发的标头表上的触发器。
我遇到的问题是触发器在COMMIT TRANSACTION
这意味着可能不会填充触发流程所需的详细信息。另外,如果系统触发ROLLBACK TRANSACTION
扳机已经扣动。
据我所知,触发器不能分配给COMMIT
,但想知道是否有人有其他想法。
我偶然发现了一个建议的 Oracle 解决方案,您可以在其中创建一个更新的物化视图ON COMMIT
,并试图找到 TSQL 等效项,即索引视图,但没有成功实现它。
总结一下:
是否可以触发COMMIT TRANSACTION
可能使用索引视图 ?
CLR http://msdn.microsoft.com/en-us/library/ms254498(v=vs.80).aspx触发代码
将 JMS 消息发送到队列,以供处理索尼克ESB http://www.progress.com/en/sonic/sonic-esb.html.
[Microsoft.SqlServer.Server.SqlTrigger(Name = "InvoiceTrigger", Target = "Table", Event = "FOR INSERT")]
public static void InvoiceTrigger()
{
//Declare Connection vairables
string connectionURL, connectionDomain, connectionUser, connectionPassword, connectionQueue;
//Constant
connectionUser = "user";
connectionPassword = "pass";
connectionQueue = "Queue";
//Local Environment
connectionURL = "tcp://IP:2506";
connectionDomain = "Domain1";
//Create connection sonic domain
SonicSend send = new SonicSend(connectionURL, connectionDomain, connectionUser, connectionPassword, connectionQueue);
//Send Test message to pipe
SqlCommand command;
SqlTriggerContext triggContext = SqlContext.TriggerContext;
SqlPipe pipe = SqlContext.Pipe;
SqlDataReader reader;
switch (triggContext.TriggerAction)
{
case TriggerAction.Insert:
// Retrieve the connection that the trigger is using
using (SqlConnection connection = new SqlConnection(@"context connection=true"))
{
connection.Open();
command = new SqlCommand(@"SELECT LSH_LINKCODE, DOC_CODE, LSH_DOCNUM FROM INSERTED;", connection);
reader = command.ExecuteReader();
if (reader.HasRows)
{
string xml;
char cr = (char)13;
int i = 0;
while (reader.Read())
{
xml = @"<Invoice action='insert'>";
xml += "<LinkCode>" + reader.GetString(0).ToString() + "</LinkCode>";
xml += "<DocumentCode>" + reader.GetString(1).ToString() + "</DocumentCode>";
xml += "<DocumentNumber>" + reader.GetString(2).ToString() + "</DocumentNumber>";
xml += @"</Invoice>";
i++;
send.testJMSsend(xml);
}
}
reader.Close();
}
break;
}
}