下面提到的sql Trigger在oracle sql plus
中正确执行 CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT ON RESULTS
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT Pd_seq.nextval INTO :NEW.PT_ID FROM dual;
END;
/
但是,当通过java实现时,相同的代码会引发以下错误
CODE:
ResultSet rs1 = stmt.executeQuery("CREATE OR REPLACE TRIGGER test_trigger123 "+
"BEFORE INSERT ON RESULTS"+
"REFERENCING NEW AS NEW"+
"FOR EACH ROW"+
"BEGIN"+
"SELECT Pd_seq.nextval INTO :NEW.PT_ID FROM dual;"+
"END;"+
"\n/");
Eclipse中的错误消息:
SQL Exception:
State : 42000
Message: ORA-04079: invalid trigger specification
Error : 4079
请帮助。导致错误的原因是什么?
答案 0 :(得分:2)
您需要在每行后插入一个空格或\ n。例如:
ResultSet rs1 = stmt.executeQuery("CREATE OR REPLACE TRIGGER test_trigger123\n"+
"BEFORE INSERT ON RESULTS\n"+
"REFERENCING NEW AS NEW\n"+
"FOR EACH ROW\n"+
"BEGIN\n"+
"SELECT Pd_seq.nextval INTO :NEW.PT_ID FROM dual;\n"+
"END;\n"+
"\n/");
答案 1 :(得分:1)
如果您尝试像现在一样执行它,它将如下所示:
stmt.executeQuery("CREATE OR REPLACE TRIGGER test_trigger123 BEFORE INSERT ON RESULTSREFERENCING NEW AS NEWFOR EACH ROWBEGINSELECT Pd_seq.nextval INTO :NEW.PT_ID FROM dual;END;\n/");
这就是为什么你必须在你的线后添加空格。
ResultSet rs1 = stmt.executeQuery("CREATE OR REPLACE TRIGGER test_trigger123 "+
"BEFORE INSERT ON RESULTS "+
"REFERENCING NEW AS NEW "+
"FOR EACH ROW "+
"BEGIN "+
"SELECT Pd_seq.nextval INTO :NEW.PT_ID FROM dual; "+
"END; "+
"\n/");