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

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

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:

CREATE TABLE Cuenta (
	fecha_creacion 	DATE NOT NULL,
	saldo_actual 	NUMBER(20,2) NOT NULL CHECK (saldo_actual >= 0),
	numero_cuenta 	NUMBER(10) NOT NULL,
	iban 		VARCHAR(24) PRIMARY KEY,
	su_banco 	VARCHAR(50) NOT NULL REFERENCES Banco(nombre_banco),
	interes 	NUMBER(4,2) CHECK (interes >= 0),
	su_codigo_oficina	NUMBER(4) REFERENCES Oficina(codigo_oficina)
);

CREATE TABLE Retirada (
	codigo_operacion 	NUMBER(38) PRIMARY KEY, 
	fecha 	 		DATE NOT NULL,
	hora 	 		TIMESTAMP NOT NULL,
	cantidad 	 	NUMBER(20,2) NOT NULL CHECK (cantidad > 0),
	descripcion 	 	VARCHAR(200),
	cuenta_emisora 		VARCHAR(24) NOT NULL REFERENCES Cuenta_corriente(iban),
	su_codigo_oficina	NUMBER(4) NOT NULL REFERENCES Oficina(codigo_oficina));

-- Valores a insertar en Cuenta
INSERT INTO Cuenta(fecha_creacion, saldo_actual, numero_cuenta, iban, su_banco, su_codigo_oficina)
VALUES (SYSDATE, 2002.00, 2345678906, 'ES1901821302192345678906', 'BBVA', '1302'); -- Cuenta_corriente

And this is the code of the trigger:

CREATE OR REPLACE TRIGGER actualizar_saldo
  AFTER INSERT ON Retirada
  FOR EACH ROW
BEGIN
  -- Actualizar el saldo de la cuenta correspondiente
  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);
END;

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

INSERT INTO Retirada (codigo_operacion, fecha, hora, cantidad, descripcion, cuenta_emisora, su_codigo_oficina)
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:

CREATE TABLE Cuenta (
	fecha_creacion 	DATE NOT NULL,
	saldo_actual 	NUMBER(20,2) NOT NULL CHECK (saldo_actual >= 0),
	numero_cuenta 	NUMBER(10) NOT NULL,
	iban 		VARCHAR(24) PRIMARY KEY,
	su_banco 	VARCHAR(50) NOT NULL REFERENCES Banco(nombre_banco),
	interes 	NUMBER(4,2) CHECK (interes >= 0),
	su_codigo_oficina	NUMBER(4) REFERENCES Oficina(codigo_oficina)
);

CREATE TABLE Retirada (
	codigo_operacion 	NUMBER(38) PRIMARY KEY, 
	fecha 	 		DATE NOT NULL,
	hora 	 		TIMESTAMP NOT NULL,
	cantidad 	 	NUMBER(20,2) NOT NULL CHECK (cantidad > 0),
	descripcion 	 	VARCHAR(200),
	cuenta_emisora 		VARCHAR(24) NOT NULL REFERENCES Cuenta_corriente(iban),
	su_codigo_oficina	NUMBER(4) NOT NULL REFERENCES Oficina(codigo_oficina));

-- Valores a insertar en Cuenta
INSERT INTO Cuenta(fecha_creacion, saldo_actual, numero_cuenta, iban, su_banco, su_codigo_oficina)
VALUES (SYSDATE, 2002.00, 2345678906, 'ES1901821302192345678906', 'BBVA', '1302'); -- Cuenta_corriente

And this is the code of the trigger:

CREATE OR REPLACE TRIGGER actualizar_saldo
  AFTER INSERT ON Retirada
  FOR EACH ROW
BEGIN
  -- Actualizar el saldo de la cuenta correspondiente
  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);
END;

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

INSERT INTO Retirada (codigo_operacion, fecha, hora, cantidad, descripcion, cuenta_emisora, su_codigo_oficina)
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

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

fiddle

英文:

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

CREATE OR REPLACE TRIGGER actualizar_saldo
  AFTER INSERT ON Retirada
  FOR EACH ROW
BEGIN
  -- Actualizar el saldo de la cuenta correspondiente
  UPDATE Cuenta
  SET   saldo_actual = saldo_actual - :NEW.cantidad
  WHERE iban = :NEW.cuenta_emisora;
END;
/

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:

确定