我的存储过程回滚得足够远吗?
我正在通过带有 odbc 连接的 php 使用 sql server 2008 数据库。
我正在读取数据文件并将它们记录到数据库中,但由于文件大小/布局不同,我的 sql 是自动生成的。
sql 按以下顺序调用:
set autocommit to off
execute some sql
execute more sql
execute a SP.
commit
在我的存储过程中,我想使用 try catch 进行错误处理,如下所示:
BEGIN try
--sql
END try
BEGIN catch
rollback
END catch
我想知道这是否会仅回滚 sp 并让我的其他 sql 提交,还是会返回到该点自动提交在哪里设置为关闭?
另一种可能的解决方案是从存储过程返回 false/true 并使用它从 php.ini 调用回滚。这可能吗?如果是这样,如何使用存储过程返回一个值?
I am working with a sql server 2008 database through php with an odbc connection.
I am reading data files and logging them into the database but due to varied file sizes/layouts, my sql is somewhat automatically generated.
The sql is called in this order:
set autocommit to off
execute some sql
execute more sql
execute a SP.
commit
in my stored procedure I want to do error handling with a try catch like so:
BEGIN try
--sql
END try
BEGIN catch
rollback
END catch
I am wondering will this roll back only the sp and leave my other sql to commit or will it go back to the point where autocommit was set to off?
another possible solution would be to return a false/true from the stored procedure and use that to call a rollback from php. is this possible? if so, how does one return a value with a stored procedure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAIK,如果您将希望回滚的所有内容包装到事务中,那么您就知道可以回滚那么远。但是,请注意您的被调用过程提交自己的事务 - 请参阅此处 了解更多。
例如,使用您提供的内容:
在这两种情况下,最终的TRANCOUNT 都应该为 0。
AFAIK, provided you wrap everything that you wish to rollback into a transaction then you know that you can rollback that far. Beware of your called procs COMMITting their own transactions, however - see here for more.
For example, using what you provided:
should end up with a
TRANCOUNT
of 0 in both cases.您正在查看嵌套事务。
请参阅此线程:复制/粘贴太多,抱歉
嵌套包含 TRY CATCH ROLLBACK 模式的存储过程?
You are looking at nested transactions.
See this thread: too much to copy/paste sorry
Nested stored procedures containing TRY CATCH ROLLBACK pattern?