SQL Server中的并发事务 [英] Concurrent Transaction in SQL server

查看:54
本文介绍了SQL Server中的并发事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

如何使用SQL在两个表中进行并发事务?
我有两个表,说tab1和tab2.我想在两个表中插入一些数据.但是,只有一个表永远不会被插入.我想确保要么插入两个表,要么都不插入.如何将事务应用于此任务????

在此先感谢

Hi there,

How to do concurrent transaction in two tables using SQL???

I''ve two tables.Say tab1 and tab2. I wanna insert some data to both the tables. But any one of the table alone never gets inserted. I wanna ensure that, either both tables get inserted or none. How to apply Transaction to this task???

Thanks in advance

推荐答案

执行以下操作
Do the following
begin transaction
insert into tab1 (col1,col2) values (1,1)
insert into tab2 (col11,col22) values (2,2)



提交并保存到两个表:



To commit and save to both tables:

commit transaction



要回滚,两个表中均不保存任何内容:



To rollback and nothing is saved in both tables:

rollback transaction


嘿, aswathy.s.88 我试图收集一些信息让您了解提交"和回滚"条件.

BEGIN TRANSACTION为发出该语句的连接启动本地事务.根据当前的事务隔离级别设置,事务将锁定为支持连接发出的Transact-SQL语句而获取的许多资源,直到事务使用COMMIT TRANSACTION或ROLLBACK TRANSACTION语句完成该事务为止.
COMMIT TRANSACTION仅在事务引用的所有数据在逻辑上都是正确的时候

ROLLBACK TRANSACTION删除从事务开始到保存点的所有数据修改.它还释放了交易所拥有的资源.
参考链接:-
COMMIT TRANSACTION(Transact-SQL) [回滚事务(Transact-SQL) [
hey aswathy.s.88 I tried to gather some information for you to let know about Commit and Rollback condition.

BEGIN TRANSACTION starts a local transaction for the connection issuing the statement. Depending on the current transaction isolation level settings, many resources acquired to support the Transact-SQL statements issued by the connection are locked by the transaction until it is completed with either a COMMIT TRANSACTION or ROLLBACK TRANSACTION statement
COMMIT TRANSACTION only at a point when all data referenced by the transaction is logically correct
and
ROLLBACK TRANSACTION erases all data modifications made from the start of the transaction or to a savepoint. It also frees resources held by the transaction.
Reference Link:-
COMMIT TRANSACTION (Transact-SQL)[^]
ROLLBACK TRANSACTION (Transact-SQL)[^]


这篇关于SQL Server中的并发事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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