事务中的 TSQL 日志记录

2022-01-06 00:00:00 logging transactions tsql sql-server

我正在尝试写入事务内的日志文件,以便即使事务回滚,日志也能保留下来.

I'm trying to write to a log file inside a transaction so that the log survives even if the transaction is rolled back.

--开始代码

开始翻译

将[东西]插入dbo.logtable

insert [something] into dbo.logtable

[[此处的主要代码]]

[[main code here]]

回滚

提交

-- 结束代码

你可以说只在事务开始之前做日志,但这并不容易,因为事务在这个 S-Proc 运行之前开始(即代码是更大事务的一部分)

You could say just do the log before the transaction starts but that is not as easy because the transaction starts before this S-Proc is run (i.e. the code is part of a bigger transaction)

所以,简而言之,有没有办法在不属于事务的事务中编写特殊语句.我希望我的问题有意义.

So, in short, is there a way to write a special statement inside a transaction that is not part of the transaction. I hope my question makes sense.

推荐答案

使用表变量 (@temp) 来保存日志信息.表变量在事务回滚后仍然存在.

Use a table variable (@temp) to hold the log info. Table variables survive a transaction rollback.

请参阅这篇文章.

相关文章