多次插入交易 [英] multi insert with transaction

查看:73
本文介绍了多次插入交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个问题,需要帮助.我有一个表单,想在树表中插入表单的信息.我可以在SQL查询中使用scope_identity()获得生成的ID,并希望在另一个表中使用该值.如果第一次插入未成功,则不会执行另一次插入.

在此先感谢您的帮助.

Hi,
I have a question and need help please. I have a form and want to insert information of form in tree table. I can get generated id with scope_identity() in SQL query and want to use that value in another table. If first insert has not successful, another insertion don''t execute.

Thanks in advance for helping.

推荐答案

启动事务,然后将代码块包装在try ... catch块中.如果第一个插入操作失败,则proc将出错(这意味着第二个将不会执行),您可以回滚该事务.

try ... catch可让您正常降级.
Start a transaction, then wrap your code block in a try...catch block. If the first insert fails the proc will error out (meaning the second one won''t execute) and you can roll back the transaction.

The try...catch lets you degrade gracefully.


这篇关于多次插入交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆