我正在尝试学习pl / sql触发器。我试图通过跟踪教程http://www.tutorialspoint.com/plsql/plsql_triggers.htm来创建一个简单的触发器,但我得到了以下错误。我在互联网上搜索但找不到解决方案。你能帮我解决这个问题吗?
CREATE OR replace TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON ok.customers
FOR EACH ROW
DECLARE
sal_diff NUMBER;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
END;
/
Trıgger DISPLAY_SALARY_CHANGES created.
SP2-0552: Bind Variable "NEW" is not declared.
PL/SQL procedure successfully completed.
编辑:我正在使用Sql Developer版本4.1.1
答案 0 :(得分:1)
试试这个:
CREATE OR replace TRIGGER test_trg
BEFORE DELETE OR INSERT OR UPDATE ON test
FOR EACH ROW
DECLARE
sal_diff NUMBER;
BEGIN
sal_diff := :new.d - :old.d;
END;
/
答案 1 :(得分:1)
请您检查一下列名。我已经尝试了下面的代码,我得到了输出。
create table test
(
no number(10),
sal number(10)
);
CREATE OR replace TRIGGER test_tr
BEFORE DELETE OR INSERT OR UPDATE ON test
FOR EACH ROW
DECLARE
sal_diff NUMBER;
BEGIN
sal_diff := :NEW.sal - :OLD.sal;
dbms_output.put_line(sal_diff);
END;
/
insert into test values(1,100);
update test set sal=200 where no=1;
输出:
1 rows inserted.
4 rows updated.
100
100
100
100
1 rows inserted.
答案 2 :(得分:1)
适用于我(例如来自您的链接,但它与您的帖子基本相同):
SQL> create table demo (id integer, salary number);
Table created.
SQL> create or replace trigger display_salary_changes
2 before delete or insert or update on demo
3 for each row
4 when (new.id > 0)
5 declare
6 sal_diff number;
7 begin
8 sal_diff := :new.salary - :old.salary;
9 dbms_output.put_line('Old salary: ' || :old.salary);
10 dbms_output.put_line('New salary: ' || :new.salary);
11 dbms_output.put_line('Salary difference: ' || sal_diff);
12 end;
13 /
Trigger created.
SQL> show errors
No errors.
SQL> insert into demo (id, salary) values (1, 100);
Old salary:
New salary: 100
Salary difference:
1 row created.
SQL> update demo set salary = salary * 1.1 where id = 1;
Old salary: 100
New salary: 110
Salary difference: 10
1 row updated.
在您的示例中显示
Trıgger DISPLAY_SALARY_CHANGES created.
它看起来不像SQL * Plus输出。你用了什么工具?
之后它会给出一个关于未定义绑定变量的SQL * Plus SP2-0552
错误,后跟
PL/SQL procedure successfully completed.
那是什么程序?我怀疑这是脚本的输出,其中一些其他步骤在创建触发器后失败。
触发器有效吗?您通常可以右键单击并检查桌面工具中的属性,或者在SQL * Plus提示符下输入
show errors trigger display_salary_changes
答案 3 :(得分:0)
我认为它缺少“按新旧换新”的句子:
CREATE TRIGGER [trigger_name]
BEFORE INSERT OR UPDATE ON [table_name]
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW