Skip to content

Instantly share code, notes, and snippets.

@emersoncortez
Created January 8, 2013 21:32
Show Gist options
  • Save emersoncortez/4488173 to your computer and use it in GitHub Desktop.
Save emersoncortez/4488173 to your computer and use it in GitHub Desktop.
Transacciones con PostgreSQL - ROLLBACK.
-- La cuenta A será cargada (se le quitará) y
-- la cuenta B será abonada (se le sumará)
-- Inicio de la transacción
BEGIN;
-- Cargando la cuenta A.
UPDATE cuentas SET saldo_cuenta = saldo_cuenta - monto WHERE nombre_cuenta = 'A';
-- Abonando la cuenta B.
UPDATE cuentas SET saldo_cuenta = saldo_cuenta + monto WHERE nombre_cuenta = 'B';
-- Asumiendo que el saldo de la cuenta A no puede ser negativo, abortamos la transacción
ROLLBACK;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment