gpt4 book ai didi

sql - 使用编译错误创建的触发器

转载 作者:行者123 更新时间:2023-12-05 00:02:34 25 4
gpt4 key购买 nike

我编写了这个触发器来在进行新购买时将数据库中的顶级客户折扣 10%:

CREATE or REPLACE TRIGGER CLIENT_DISCOUNT
BEFORE INSERT
ON PURCHASE
FOR EACH ROW
DECLARE
CLIENTNO NUMBER(5);
BEGIN
SELECT (SELECT CLIENT.CLIENTNO, CLIENT.CNAME, TOTALS.TOTAL FROM CLIENT,
(SELECT CLIENTNO, SUM(AMOUNT) AS TOTAL FROM PURCHASE GROUP BY CLIENTNO) TOTALS WHERE CLIENT.CLIENTNO = TOTALS.CLIENTNO AND ROWNUM <= 1 ORDER BY TOTALS.TOTAL DESC).CLIENTNO INTO CLIENTNO;
IF :NEW.CLIENTNO = CLIENTNO THEN
:NEW.AMOUNT = (:NEW.AMOUNT * 0.1);
END IF;
END;

但是,当我执行此语句时,我收到此消息:
Warning: Trigger created with compilation errors.

有人可以告诉我我做错了什么吗?

谢谢,
亚历克斯。

更新 - 错误:
Errors for TRIGGER CLIENT_DISCOUNT:

LINE/COL
--------------------------------------------------------------------------------
ERROR
--------------------------------------------------------------------------------
4/3
PL/SQL: SQL Statement ignored

5/141
PL/SQL: ORA-00907: missing right parenthesis

7/17
PLS-00103: Encountered the symbol "=" when expecting one of the following:


LINE/COL
--------------------------------------------------------------------------------
ERROR
--------------------------------------------------------------------------------
:= . ( @ % ; indicator

8/3
PLS-00103: Encountered the symbol "END"

解决方案:
CREATE or REPLACE TRIGGER CLIENT_DISCOUNT
BEFORE INSERT
ON PURCHASE
FOR EACH ROW
DECLARE
vCLIENTNO NUMBER(5);
BEGIN
SELECT TOPCLIENT.CLIENTNO INTO vCLIENTNO FROM (SELECT CLIENT.CLIENTNO, CLIENT.CNAME, TOTALS.TOTAL FROM CLIENT, (SELECT CLIENTNO, SUM(AMOUNT) AS TOTAL FROM PURCHASE GROUP BY CLIENTNO) TOTALS WHERE CLIENT.CLIENTNO = TOTALS.CLIENTNO AND ROWNUM <= 1 ORDER BY TOTALS.TOTAL DESC) TOPCLIENT;
IF :NEW.CLIENTNO = vCLIENTNO THEN
:NEW.AMOUNT := (:NEW.AMOUNT * 0.9);
END IF;
END;
/

最佳答案

我手头没有你的 table ,所以我不能保证我已经找到了你所有的错误。但是,我可以说以下几点:

  • 我不相信你能做到SELECT (....).CLIENTNO .试试 SELECT x.CLIENTNO FROM (....) x反而。
  • 您的最外层 SELECT没有 FROM条款。尝试添加 FROM DUAL ,因为这个最外层 SELECT不从任何表中选择。
  • PL/SQL 赋值运算符是 := ,不是 = .分配给 :NEW.AMOUNT ,你需要写:NEW.AMOUNT := (:NEW.AMOUNT * 0.1); .
  • 将金额乘以 0.1 会给客户 90% 的折扣,而不是 10% 的折扣。
  • 关于sql - 使用编译错误创建的触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7818276/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com