2

下面提到在oracle sql plus中正确执行的sql Trigger

 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实现时,抛出如下错误

代码:

 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

请协助。是什么导致了错误?

4

2 回答 2

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/");
于 2013-07-10T07:04:59.580 回答
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/");
于 2013-07-10T07:08:08.313 回答