# SQLite Transaction over two tables

**URL:** <https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205>\
**Category:** Databases\
**Created:** [September 6, 2018, 9:27pm UTC](https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205 "2018-09-06T21:27:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ralph\_Alvy](https://forum.xojo.com/user_avatar/forum.xojo.com/ralph_alvy/32/4528_2.png) [@Ralph\_Alvy](https://forum.xojo.com/u/Ralph_Alvy)\
**Post date:** [September 6, 2018, 9:27pm UTC](https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205/1 "2018-09-06T21:27:50Z")

</div>

Can I have a single Transaction wrap changes to two tables? That is, I do something like this:

Begin Transaction  
delete rows in Table 1  
If error then  
Rollback  
Return  
End If  
delete rows in Table 2  
If error then  
Rollback  
Return  
End If  
Commit

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://forum.xojo.com/user_avatar/forum.xojo.com/neil_burkholder/32/2189_2.png) [@Neil\_Burkholder](https://forum.xojo.com/u/Neil_Burkholder)\
**Post date:** [September 6, 2018, 9:29pm UTC](https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205/2 "2018-09-06T21:29:54Z")

</div>

Sure can.

---

<div class="post-metadata">

**Author:** ![Ralph\_Alvy](https://forum.xojo.com/user_avatar/forum.xojo.com/ralph_alvy/32/4528_2.png) [@Ralph\_Alvy](https://forum.xojo.com/u/Ralph_Alvy)\
**Post date:** [September 6, 2018, 9:31pm UTC](https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205/3 "2018-09-06T21:31:00Z")

</div>

> [@404425:@Neil Burkholder](#):
>
> Sure can.

I was worried the Rollback would only cover the most recent table.

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://forum.xojo.com/user_avatar/forum.xojo.com/neil_burkholder/32/2189_2.png) [@Neil\_Burkholder](https://forum.xojo.com/u/Neil_Burkholder)\
**Post date:** [September 6, 2018, 9:34pm UTC](https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205/4 "2018-09-06T21:34:03Z")

</div>

Nope that’s what transactions are for.

---

<div class="post-metadata">

**Author:** ![Greg\_O\_Lone](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o_lone/32/49_2.png) [@Greg\_O\_Lone](https://forum.xojo.com/u/Greg_O_Lone)\
**Post date:** [September 7, 2018, 12:10pm UTC](https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205/5 "2018-09-07T12:10:13Z")

</div>

The whole point of a transaction is to allow you to send multiple interrelated commands to the database and roll them all back if just one fails.

For example, imagine you were a bank. To move money from one account (table) to another, you need two sql commands. One to remove the amount from table A and another to add the amount to table B. If either of those commands were to fail, youd be in big trouble. Without a transaction an error after the first one is easy to fix, but if the second fails, you would have fix whatever went wrong in table B and then go back and reinsert money into table A. Using a transaction, you just issue a Rollback and everything back to the Start Transaction is undone automatically.

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [September 7, 2018, 2:32pm UTC](https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205/6 "2018-09-07T14:32:42Z")

</div>

More background:

[http://developer.xojo.com/userguide/database-transactions](http://developer.xojo.com/userguide/database-transactions)

---

<div class="post-metadata">

**Author:** ![Ralph\_Alvy](https://forum.xojo.com/user_avatar/forum.xojo.com/ralph_alvy/32/4528_2.png) [@Ralph\_Alvy](https://forum.xojo.com/u/Ralph_Alvy)\
**Post date:** [September 7, 2018, 2:50pm UTC](https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205/7 "2018-09-07T14:50:06Z")

</div>

Thanks, all. I have no idea when I started thinking transactions might be restricted to a single table. This thread really helped.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 7:21pm UTC](https://forum.xojo.com/t/sqlite-transaction-over-two-tables/45205/8 "2020-10-29T19:21:43Z")

</div>


