2013-10-03 70 views
0

我創建了甲骨文展示一個觸發器:錯誤與SQL CREATE TRIGGER甲骨文

/* Formatted on 3-Oct-2013 15:58:45 (QP5 v5.126) */ 
CREATE OR REPLACE TRIGGER testtrigger 
AFTER INSERT OR UPDATE OF sellpoint_name 
ON sell_point 
FOR EACH ROW 
WHEN (new.sellpoint_name = 'Location') 
DECLARE lat, lng sell_point.sellpoint_lat%TYPE; 
BEGIN 
SELECT sellpoint_lat, sellpoint_long into lat, lng 
    FROM sell_point 
WHERE sellpoint_name = :new.sellpoint_name; 

IF (:new.sellpoint_lat < 20 OR :new.sellpoint_long < 100) 
THEN 
    raise_application_error (-20225, 'this point is not exists'); 
END IF; 
END; 

,但我得到一個錯誤:

1/12 PLS-00103: Encountered the symbol "," when expecting one of the following: 
constant exception <an identifier> 
<a double-quoted delimited-identifier> table long double ref 
char time timestamp interval date binary national character 
nchar 
1/47 PLS-00103: Encountered the symbol ";" when expecting one of the following: 
16:10:50   := (, not null range default external character 

什麼錯在這裏?感謝幫助!

回答

2

沒有數據類型嘗試這樣,

CREATE OR REPLACE TRIGGER testtrigger 
AFTER INSERT OR UPDATE OF sellpoint_name ON sell_point 
FOR EACH ROW 
WHEN (new.sellpoint_name = 'Location') 
DECLARE 
    lat sell_point.sellpoint_lat%TYPE; 
    lng sell_point.sellpoint_long%TYPE; 
BEGIN 
    SELECT sellpoint_lat, sellpoint_long 
     INTO lat, lng 
     FROM sell_point 
     WHERE sellpoint_name = :new.sellpoint_name; 

    IF (:NEW.sellpoint_lat < 20 OR :NEW.sellpoint_long < 100) THEN 
      raise_application_error (-20225, 'this point is not exists'); 
    END IF; 
END; 

編輯

上面的代碼將引發錯誤,table is mutating,如您正試圖從同一張表中選擇列。

您可以編輯你像這樣的代碼,

CREATE OR REPLACE TRIGGER testtrigger 
AFTER INSERT OR UPDATE OF sellpoint_name ON sell_point 
FOR EACH ROW 
WHEN (new.sellpoint_name = 'Location') 
BEGIN 
    IF (:NEW.sellpoint_lat < 20 OR :NEW.sellpoint_long < 100) THEN 
      raise_application_error (-20225, 'this point is not exists'); 
    END IF; 
END; 
+0

它工作!謝謝! – Thangnv

+1

但是爲什麼要從表格中選擇sellpoint_lat和sellpoint_long列,而不是隻能使用:new.sellpoint_lat和:new.sellpoint_long。 否則您將得到t他錯誤,表格在變化。 – Dba

+0

我想在插入sellpoint_lat <20或sellpoint_long <100後執行操作。不在意「raise_application_error(-20225,'這一點不存在');」。 Actualy我有一個錯誤變異。任何更多的解釋? – Thangnv

0

我想是因爲你有LAT

DECLARE lat, lng sell_point.sellpoint_lat%TYPE; 
     ^
+0

我嘗試,但還是錯誤: 16時十九分28秒ORA-24344:與編譯錯誤成功 16時十九分28秒1/42 PLS-00103 :遇到符號「,」期待以下某一項時: 16:19:28:=(;非空範圍默認字符 – Thangnv

+0

對不起,別看別的:-( – asantaballa