t1f
t1f

Reputation: 3181

Wrap SQL statements in transaction

I am using the following:

begin
dbmodule.comenziQuery.SQL.Clear;
dbmodule.comenziQuery.SQL.Add('INSERT INTO `r33758pi_tipotask`.`arhiva` SELECT id, data, stare, client, telefon, email, detalii, observatii, pret, livrare, user, status FROM comenzi WHERE `id`='''+inttostr(dbmodule.comenziDataSetid.Value)+''';');
dbmodule.comenziQuery.ExecSQL(true);

dbmodule.comenziQuery.SQL.Clear;
dbmodule.comenziQuery.SQL.Add('DELETE FROM `r33758pi_tipotask`.`comenzi` WHERE `id`='''+inttostr(dbmodule.comenziDataSetid.Value)+''';');
dbmodule.comenziQuery.ExecSQL(true);
end;

Can anyone guide me on how to wrap this in a transaction? I've never used one before and I'm having difficulty understanding how to modify the code to add it.

I'm trying to add it in a transaction as I've read it is good practice, since if 1 fails it won't execute at all.

I've tried following this guide Some Guide and subsequently using the begin transaction and begin try that are mentioned there but I'm guessing that's not intented for delphi code, or something..so no luck.

Hope the code is ok, please feel free to point out anything wrong I might have done so far.

Delphi, using Rad Studio 10 Seattle, TSQLQuery - Server is mysql - let me know in a comment if I left anything out and I will edit.

Upvotes: 5

Views: 2739

Answers (1)

OnoMrBill
OnoMrBill

Reputation: 66

Transactions are usually used within SQL Server stored procedures where you have a block of sql statements between the BEGIN TRANSACTION and COMMIT TRANSACTION or ROLLBACK TRANSACTION statements.

Not sure if it works the same for MySQL.

Transactions are usually handled on the sql server, as opposed to doing it from code.

Upvotes: 2

Related Questions