如何解决我的扳机有什么问题? Oracle SQLPLUS
sqlPLUS触发编译错误
我是oracle sqlplus的新手,我试图创建一个触发器,该触发器在客户从其帐户存款或取款时触发,其中显示account_no,交易类型(存款/取款),交易金额,旧余额和新余额:
这是表格(我已经创建)的代码:
CREATE TABLE CUSTOMERS,(
custname VARCHAR2(20)NOT NULL,account_no NUMBER(20)NOT NULL,cur_balance NUMBER(20) NOT NULL,PRIMARY KEY (account_no)
);
CREATE OR REPLACE TRIGGER account_tx
AFTER INSERT OR UPDATE OF cur_balance ON customers
FOR EACH ROW
DECLARE
new_balance number;
BEGIN CASE WHEN :NEW.cur_balance <:OLD.cur_balance THEN 'WITHDRAWAL';
ELSE 'DEPOSIT';
new_balance := :NEW.cur_balance - :OLD.cur_balance;
dbms_output.put_line('Old balance: ' || :OLD.cur_balance);
dbms_output.put_line('New balance: ' || :NEW.cur_balance);
dbms_output.put_line('Transaction Amount: ' || new_balance);
END CASE;
END;
/
解决方法
您的创建表无效,用逗号隔开第一行
收到错误后,可以使用“显示错误”命令查看它们。
运行您的代码,我看到了:
SQL> show errors
Errors for TRIGGER ACCOUNT_TX:
LINE/COL ERROR
-------- -----------------------------------------------------------------
3/57 PLS-00103: Encountered the symbol "WITHDRAWAL" when expecting one
of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
json_exists json_value json_query json_object json_array
SQL>
,
您的CASE语句被弄乱了,您需要将CASE语句的结果分配给一个变量,如下所示:
CREATE OR REPLACE TRIGGER account_tx
AFTER INSERT OR UPDATE OF cur_balance ON customers
FOR EACH ROW
DECLARE
new_balance number;
trans_type varchar2(30);
BEGIN
trans_type := CASE WHEN :NEW.cur_balance <:OLD.cur_balance THEN 'WITHDRAWAL'ELSE 'DEPOSIT' END;
new_balance := :NEW.cur_balance - :OLD.cur_balance;
dbms_output.put_line('Old balance: ' || :OLD.cur_balance);
dbms_output.put_line('New balance: ' || :NEW.cur_balance);
dbms_output.put_line('Transaction Amount: ' || new_balance);
dbms_output.put_line('trans type: ' || trans_type);
END;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。