Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jatuphum
Starting Member
1 Post |
Posted - 2009-08-17 : 07:45:15
|
| Hi all,I want to use transaction with my while loop program.Because with per loop, I want to Update A Table and Insert B Table.While @start <= @numberBEGIN Update TableA SET fieldA1 = @valueA1, fieldA2 = @valueA2 Insert into TableB (fieldB1, fieldB2) values (@valueB1, @valueB2) SET @start = @start + 1ENDHow can I apply transaction with While loop?Because if any error occur with once loop, I want to rollback previous Update and Insert statement.Thank you very much. |
|
|
Sachin.Nand
2937 Posts |
Posted - 2009-08-17 : 07:49:55
|
| BEGIN TRYBEGIN TRANSACTIONWhile @start <= @numberBEGINUpdate TableA SET fieldA1 = @valueA1, fieldA2 = @valueA2Insert into TableB (fieldB1, fieldB2) values (@valueB1, @valueB2)ENDCOMMITEND TRYBEGIN CATCHIF @@TRANCOUNT<>0BEGIN ROLLBACKSELECT ERROR_MESSAGE()ENDEND CATCHPBUH |
 |
|
|
|
|
|