Sto imparando transazioni MySQL. Ho cercato risposte a questo, ma sembrano tutti usare PHP per fare ciò che voglio fare lavorare. Ecco l'esempio di quello che sto cercando di fare:come sapere se un inserto mysql ha successo
- Iniziare la transazione
- aggiornamento Table1
- Inserire nel Table2
- Se Insert è successo, a. quindi inserire in Table3 e commit. b. altrimenti rollback della transazione.
Non capisco come determinare a livello di programmazione se l'inserimento nel passaggio 3 ha avuto esito positivo. Certo, posso interrogare la tabella e vedere, ma ho pensato che ci fosse un modo per usare il valore di ritorno, ma sembra che funzioni solo quando sto usando PHP per fare la transazione.
Questo è il blocco di codice per quello che sto cercando - non funziona:
begin;
start transaction;
-- attempt to reduce inventory
update store_inventory set item_qty = item_qty - 2 where id = 1;
update store_inventory set item_qty = item_qty -1 where id = 5;
-- insert the order record and check whether it succeded
insert into store_orders (purchaser_name, purchase_date)
values ('test user', now());
-- if successful, do final insert and commit
if Row_Count() > 0 Then
insert into store_inventory (order_id, inventory_id, item_qty)
values (1, 1, 2),
(1, 2, 1);
commit;
else -- otherwise rollback
rollback;
end if;
end;
Quale lingua e adattatore stai utilizzando per provare ed eseguire queste azioni? –
Utilizzo editor di query MySQL Workbench. –
Quindi stai provando a vedere se l'inserimento ha avuto successo in una seguente query? –