Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TSQL:提交时触发_Sql_Sql Server_Tsql_Esb - Fatal编程技术网

TSQL:提交时触发

TSQL:提交时触发,sql,sql-server,tsql,esb,Sql,Sql Server,Tsql,Esb,我们有一个后台系统,它通过存储过程将发票信息插入SQL数据库,应用程序插入标题,然后插入详细信息 我在头表上设置了一个触发器,当插入记录时触发该触发器 我遇到的问题是触发器在提交事务之前触发,这意味着可能不会填充详细信息,这是触发的进程所必需的。此外,如果系统触发回滚事务,则触发器已经触发 我知道触发器不能分配给提交,但我想知道是否有人有其他想法 我偶然发现了一个针对oracle的建议解决方案,其中您创建了一个Materlised视图,在提交时更新,,并试图找到与TSQL等价的索引视图,但没有实

我们有一个后台系统,它通过存储过程将发票信息插入SQL数据库,应用程序插入标题,然后插入详细信息

我在头表上设置了一个触发器,当插入记录时触发该触发器

我遇到的问题是触发器在
提交事务之前触发,这意味着可能不会填充详细信息,这是触发的进程所必需的。此外,如果系统触发
回滚事务
,则触发器已经触发

我知道触发器不能分配给
提交
,但我想知道是否有人有其他想法

我偶然发现了一个针对oracle的建议解决方案,其中您创建了一个Materlised视图,在提交时更新
,并试图找到与TSQL等价的索引视图,但没有实现它

总结如下:
是否可以使用索引视图在提交事务时触发


触发代码

将JMS消息发送到队列,以供处理

[Microsoft.SqlServer.Server.SqlTrigger(Name=“InvoiceTrigger”,Target=“Table”,Event=“FOR INSERT”)]
公共静态void InvoiceTrigger()
{
//声明连接变量
字符串connectionURL、connectionDomain、connectionUser、connectionPassword、connectionQueue;
//不变的
connectionUser=“用户”;
connectionPassword=“通过”;
connectionQueue=“队列”;
//当地环境
connectionURL=”tcp://IP:2506";
connectionDomain=“域1”;
//创建音域连接
SonicSend=新的SonicSend(connectionURL、connectionDomain、connectionUser、connectionPassword、connectionQueue);
//将测试消息发送到管道
SqlCommand命令;
SqlTriggerContext triggContext=SqlContext.TriggerContext;
SqlPipe=SqlContext.pipe;
SqlDataReader;
开关(triggContext.TriggerAction)
{
案例触发操作。插入:
//检索触发器正在使用的连接
使用(SqlConnection=newsqlconnection(@“context connection=true”))
{
connection.Open();
command=new SqlCommand(@“选择LSH_链接代码、文档代码、LSH_文档编号从插入位置开始;”,连接);
reader=command.ExecuteReader();
if(reader.HasRows)
{
字符串xml;
char cr=(char)13;
int i=0;
while(reader.Read())
{
xml=@;
xml++=“reader.GetString(0.ToString()+”;
xml++=“reader.GetString(1.ToString()+”;
xml++=“reader.GetString(2.ToString()+”;
xml+=@;
i++;
testJMSsend(xml);
}
}
reader.Close();
}
打破
}
}

参见本文,您可能需要使用上下文信息,然后为这些记录触发触发器


在一些有用的评论之后,这些评论指出在触发器中使用提交是不可能的

我查看了由trigger启动的ESB进程,该进程现在使用传递的信息查询数据,这解决了我的问题,因为
开始事务
会对数据创建锁,直到出现
提交事务
,查询才会返回结果,因此,这意味着在查询返回数据之前,该过程不会继续,因此将始终填充发票详细信息


另外,如果有一个
回滚事务
,进程将超时,并按预期抛出错误。

为什么不将代码放在触发器中,并将其放在事务中?在这种情况下,您需要重新考虑代码。在触发导致触发器的insert/update之前,您需要拥有所有信息(可能在保留表中)。您没有发布过如此难以帮助的代码。回滚还将回滚事务期间触发器所做的任何更改。触发器由一个操作触发—
插入
更新
删除
。因此,否-您不能将触发器“分配”到
COMMIT
-您需要将其分配给添加了CLR代码的操作之一(或其组合),回滚的问题是C#已经执行。
[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;
    }

}