TSQL:提交时触发

2024-04-10

我们有一个后台系统,可以通过以下方式将发票信息插入到 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;
    }

}

在有用的评论指出不可能在触发器中使用提交之后。

我查看了由触发器启动的 ESB 流程,该流程现在使用传递的信息查询数据,这解决了我的问题,因为BEGIN TRANSACTION对数据创建锁定,查询不会返回结果,直到出现COMMIT TRANSACTION,这意味着在查询返回数据之前该过程不会继续,因此发票详细信息将始终被填充。

另外如果有一个ROLLBACK TRANSACTION,该过程将按预期超时并抛出错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TSQL:提交时触发 的相关文章

随机推荐