我正在使用 Java 访问 Oracle 数据库。
我创建了一个 SQL 函数,它根据用户的交易(购买或销售)计算用户的反馈。
反馈值存储在表中TRANSACCOES
。当我更新反馈列时,该函数工作正常,但是当我在“删除”后立即重新计算反馈时(我并没有真正删除它,只是设置一个标记为已删除),我收到一个错误:ORA-01476: divisor is equal to zero.
如果在该错误之后我对另一个事务进行更新,它会重新计算就好了。所以我不知道为什么会这样。
这是功能:
CREATE OR REPLACE FUNCTION calcula_classificacao (id_user NUMBER
) RETURN FLOAT AS
counter NUMBER;
sumvendr FLOAT;
sumcompr FLOAT;
f FLOAT;
f1 FLOAT;
BEGIN
SELECT COUNT(t.ID_TRANSACCAO) INTO counter
FROM TRANSACCOES t
JOIN ARTIGOS a
ON a.ID_ARTIGO = t.ID_ARTIGO
WHERE ((t.ID_COMPRADOR = id_user AND t.FEEDBACK_COMPRADOR >= 0)
OR (id_user = a.ID_VENDEDOR AND t.FEEDBACK_VENDEDOR >= 0))
AND t.ELIMINADO = 0;
SELECT SUM(t.FEEDBACK_COMPRADOR) INTO sumcompr
FROM TRANSACCOES t
JOIN ARTIGOS a
ON a.ID_ARTIGO = t.ID_ARTIGO
WHERE t.ID_COMPRADOR = id_user
AND t.ELIMINADO = 0
AND t.FEEDBACK_COMPRADOR >= 0;
SELECT SUM(t.FEEDBACK_VENDEDOR) INTO sumvendr
FROM TRANSACCOES t
JOIN ARTIGOS a
ON a.ID_ARTIGO = t.ID_ARTIGO
WHERE id_user = a.ID_VENDEDOR
AND t.ELIMINADO = 0
AND t.FEEDBACK_VENDEDOR >= 0;
IF (sumvendr IS NULL) THEN
sumvendr := 0;
END IF;
IF (sumcompr IS NULL) THEN
sumcompr := 0;
END IF;
sumvendr := sumvendr + sumcompr;
f := (sumvendr*100);
f1 := f/counter;
UPDATE utilizadores SET classificacao = f1 WHERE id_utilizador = id_user;
RETURN f1;
END;