Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Share this answer
aswathy.s.88 14-Oct-11 2:46am    
What should be the condition to give to execute Commit and Rollback?
Mehdi Gholam 14-Oct-11 2:53am    
That depends on you and your business requirements.
RaviRanjanKr 14-Oct-11 2:55am    
Take a look at my answer to know about Commit and Rollback condition.
RaviRanjanKr 14-Oct-11 2:48am    
My 5+
Mehdi Gholam 14-Oct-11 2:53am    
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
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:-
Share this answer
Mehdi Gholam 14-Oct-11 4:43am    
My 5!
RaviRanjanKr 14-Oct-11 9:24am    
Thanks Mehdi.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900