SQL Error [4098] [42000]: ORA-04098在尝试在触发器之后插入数据时发生错误。

huangapple go评论100阅读模式
英文:

SQL Error [4098] [42000]: ORA-04098 when trying to insert data after trigger

问题

I'm making an oracle database, and all inserts in my tables work fine. But after I run the trigger and try to do another inserts, oracle shows an error. This is the table of "Cuenta" and of "Retirada" and insert:

  1. CREATE TABLE Cuenta (
  2. fecha_creacion DATE NOT NULL,
  3. saldo_actual NUMBER(20,2) NOT NULL CHECK (saldo_actual >= 0),
  4. numero_cuenta NUMBER(10) NOT NULL,
  5. iban VARCHAR(24) PRIMARY KEY,
  6. su_banco VARCHAR(50) NOT NULL REFERENCES Banco(nombre_banco),
  7. interes NUMBER(4,2) CHECK (interes >= 0),
  8. su_codigo_oficina NUMBER(4) REFERENCES Oficina(codigo_oficina)
  9. );
  10. CREATE TABLE Retirada (
  11. codigo_operacion NUMBER(38) PRIMARY KEY,
  12. fecha DATE NOT NULL,
  13. hora TIMESTAMP NOT NULL,
  14. cantidad NUMBER(20,2) NOT NULL CHECK (cantidad > 0),
  15. descripcion VARCHAR(200),
  16. cuenta_emisora VARCHAR(24) NOT NULL REFERENCES Cuenta_corriente(iban),
  17. su_codigo_oficina NUMBER(4) NOT NULL REFERENCES Oficina(codigo_oficina));
  18. -- Valores a insertar en Cuenta
  19. INSERT INTO Cuenta(fecha_creacion, saldo_actual, numero_cuenta, iban, su_banco, su_codigo_oficina)
  20. VALUES (SYSDATE, 2002.00, 2345678906, 'ES1901821302192345678906', 'BBVA', '1302'); -- Cuenta_corriente

And this is the code of the trigger:

  1. CREATE OR REPLACE TRIGGER actualizar_saldo
  2. AFTER INSERT ON Retirada
  3. FOR EACH ROW
  4. BEGIN
  5. -- Actualizar el saldo de la cuenta correspondiente
  6. UPDATE Cuenta SET saldo_actual = saldo_actual - (SELECT cantidad FROM retirada WHERE codigo_operacion = :NEW.retirada) WHERE iban = (SELECT cuenta_emisora FROM retirada WHERE codigo_operacion = :NEW.retirada);
  7. END;

It's supposed to extract money from "Cuenta". And after running the trigger, if I try and do:

  1. INSERT INTO Retirada (codigo_operacion, fecha, hora, cantidad, descripcion, cuenta_emisora, su_codigo_oficina)
  2. VALUES (9, SYSDATE, CURRENT_TIMESTAMP, 250, 'Retirada 5', 'ES1901821302192345678906', '1302');

Oracle gives me this error, but what it should do is extract 250 from a specific "Cuenta". I can't seem to find why. Thanks for your help.

英文:

I'm making an oracle database, and all inserts in my tables work fine. But after I run the trigger and try to do another inserts, oracle shows an error. This is the table of "Cuenta" and of "Retirada" and insert:

  1. CREATE TABLE Cuenta (
  2. fecha_creacion DATE NOT NULL,
  3. saldo_actual NUMBER(20,2) NOT NULL CHECK (saldo_actual >= 0),
  4. numero_cuenta NUMBER(10) NOT NULL,
  5. iban VARCHAR(24) PRIMARY KEY,
  6. su_banco VARCHAR(50) NOT NULL REFERENCES Banco(nombre_banco),
  7. interes NUMBER(4,2) CHECK (interes >= 0),
  8. su_codigo_oficina NUMBER(4) REFERENCES Oficina(codigo_oficina)
  9. );
  10. CREATE TABLE Retirada (
  11. codigo_operacion NUMBER(38) PRIMARY KEY,
  12. fecha DATE NOT NULL,
  13. hora TIMESTAMP NOT NULL,
  14. cantidad NUMBER(20,2) NOT NULL CHECK (cantidad > 0),
  15. descripcion VARCHAR(200),
  16. cuenta_emisora VARCHAR(24) NOT NULL REFERENCES Cuenta_corriente(iban),
  17. su_codigo_oficina NUMBER(4) NOT NULL REFERENCES Oficina(codigo_oficina));
  18. -- Valores a insertar en Cuenta
  19. INSERT INTO Cuenta(fecha_creacion, saldo_actual, numero_cuenta, iban, su_banco, su_codigo_oficina)
  20. VALUES (SYSDATE, 2002.00, 2345678906, 'ES1901821302192345678906', 'BBVA', '1302'); -- Cuenta_corriente

And this is the code of the trigger:

  1. CREATE OR REPLACE TRIGGER actualizar_saldo
  2. AFTER INSERT ON Retirada
  3. FOR EACH ROW
  4. BEGIN
  5. -- Actualizar el saldo de la cuenta correspondiente
  6. UPDATE Cuenta SET saldo_actual = saldo_actual - (SELECT cantidad FROM retirada WHERE codigo_operacion = :NEW.retirada) WHERE iban = (SELECT cuenta_emisora FROM retirada WHERE codigo_operacion = :NEW.retirada);
  7. END;

It's supposed to extract money from "Cuenta". And after running the trigger, if I try and do:

  1. INSERT INTO Retirada (codigo_operacion, fecha, hora, cantidad, descripcion, cuenta_emisora, su_codigo_oficina)
  2. VALUES (9, SYSDATE, CURRENT_TIMESTAMP, 250, 'Retirada 5', 'ES1901821302192345678906', '1302');

Oracle gives me this error, but what it should do is extract 250 from an specific "Cuenta". I can't seem to find why. Thanks for your help.

答案1

得分: 0

你似乎想要使用:NEW记录,而不是尝试从表中进行SELECT

  1. 创建或替换触发器 actualizar_saldo
  2. Retirada 上插入后
  3. 对于每个行
  4. 开始
  5. -- 更新相应帐户的余额
  6. 更新 Cuenta
  7. 设置 saldo_actual = saldo_actual - :NEW.cantidad
  8. 其中 iban = :NEW.cuenta_emisora;
  9. 结束;
  10. /

fiddle

英文:

You appear to want to use the :NEW record rather than trying to SELECT from the table:

  1. CREATE OR REPLACE TRIGGER actualizar_saldo
  2. AFTER INSERT ON Retirada
  3. FOR EACH ROW
  4. BEGIN
  5. -- Actualizar el saldo de la cuenta correspondiente
  6. UPDATE Cuenta
  7. SET saldo_actual = saldo_actual - :NEW.cantidad
  8. WHERE iban = :NEW.cuenta_emisora;
  9. END;
  10. /

fiddle

huangapple
  • 本文由 发表于 2023年3月21日 03:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794373-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定