如何确保嵌套事务彼此独立提交?

发布于 2024-10-10 19:19:25 字数 446 浏览 1 评论 0原文

如果我有一个存储过程使用不同的参数多次执行另一个存储过程,是否可以让每个调用独立于其他调用提交?

换句话说,如果嵌套过程的前两次执行成功,但第三次执行失败,是否可以保留前两次执行的结果(而不是回滚它们)?

我在 SQL Server 2000 中定义了一个类似这样的存储过程:

CREATE PROCEDURE toplevel_proc ..
AS
BEGIN

         ...

         while @row_count <= @max_rows
    begin
        select @parameter ... where rownum = @row_count 
        exec nested_proc @parameter
        select @row_count = @row_count + 1
    end

END

If I have a stored procedure that executes another stored procedure several times with different arguments, is it possible to have each of these calls commit independently of the others?

In other words, if the first two executions of the nested procedure succeed, but the third one fails, is it possible to preserve the results of the first two executions (and not roll them back)?

I have a stored procedure defined something like this in SQL Server 2000:

CREATE PROCEDURE toplevel_proc ..
AS
BEGIN

         ...

         while @row_count <= @max_rows
    begin
        select @parameter ... where rownum = @row_count 
        exec nested_proc @parameter
        select @row_count = @row_count + 1
    end

END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放手` 2024-10-17 19:19:25

首先,SQL Server 中没有嵌套事务这样的东西

但是,您可以使用 SAVEPOINTs 按照此示例(抱歉,太长,无法在此处重现)来自其他 SO 用户 Remus Rusanu

编辑:AlexKuznetsov 提到(尽管他删除了他的答案)如果交易注定失败,这将不起作用。当 SET XACT_ABORT ON 或某些触发错误时,可能会发生这种情况。

First off, there is no such thing as a nested transaction in SQL Server

However, you can use SAVEPOINTs as per this example (too long to reproduce here sorry) from fellow SO user Remus Rusanu

Edit: AlexKuznetsov mentioned (he deleted his answer though) that this won't work if a transaction is doomed. This can happen with SET XACT_ABORT ON or some trigger errors.

对风讲故事 2024-10-17 19:19:25

来自博尔:

不带回滚事务
保存点名称或事务名称
回滚到开头
交易。筑巢时
交易,同样的声明
将所有内部事务回滚到
最外面的 BEGIN TRANSACTION
声明。

我还从另一个线程这里找到了以下内容

请注意,SQL Server 事务
并没有真正以你的方式嵌套
可能会想。一旦明确
事务开始后,后续的
BEGIN TRAN 增量 @@TRANCOUNT
而 COMMIT 则减少该值。
整个最外面的交易是
当 COMMIT 产生结果时提交
零@@TRANCOUNT。但回滚
没有保存点则回滚所有
工作包括最外面的
交易。

如果需要嵌套事务
行为,您需要使用 SAVE
TRANSACTION 而不是 BEGIN TRAN 和
使用 ROLLBACK TRAN [保存点名称]
而不是 ROLLBACK TRAN。

所以这看起来是可能的。

From BOL:

ROLLBACK TRANSACTION without a
savepoint_name or transaction_name
rolls back to the beginning of the
transaction. When nesting
transactions, this same statement
rolls back all inner transactions to
the outermost BEGIN TRANSACTION
statement.

I also found the following from another thread here:

Be aware that SQL Server transactions
aren't really nested in the way you
might think. Once an explict
transaction is started, a subsequent
BEGIN TRAN increments @@TRANCOUNT
while a COMMIT decrements the value.
The entire outmost transaction is
committed when a COMMIT results in a
zero @@TRANCOUNT. But a ROLLBACK
without a savepoint rolls back all
work including the outermost
transaction.

If you need nested transaction
behavior, you'll need to use SAVE
TRANSACTION instead of BEGIN TRAN and
use ROLLBACK TRAN [savepoint_name]
instead of ROLLBACK TRAN.

So it would appear possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文