2014-01-25 83 views
0

變量如何聲明temp變量,雖然我根據我已經學習語法,而且,它是正確的,但後來太,它顯示錯誤:Undefined temp and t.宣言觸發

CREATE TRIGGER check_reciever BEFORE INSERT 
ON one_to_one FOR each row 
BEGIN 
select employee_id into temp from employee_master where employee_id = NEW.reciever_id; 
IF temp!=0 THEN 
insert into one_to_one values(new.message_id,new.reciever_id,new.read); 
ELSE 
    select count(*) into t from parent_master where parent_id=new.reciever_id; 
    IF t!=0 THEN 
    insert into one_to_one values(new.message_id,new.reciever_id,new.read); 
    END IF; 
END IF; 
END 

回答

0

你錯過了其被用於聲明變量(如可從它的名字被猜到)的DECLARE子句:

CREATE TRIGGER check_reciever BEFORE INSERT 
ON one_to_one FOR each row 
-- Begining of the DECLARE section you're missing 
DECLARE 
    temp NUMBER; 
    t NUMBER; 
-- End of the missing section 
BEGIN 
    select employee_id into temp from employee_master where employee_id = NEW.reciever_id; 
    IF temp!=0 THEN 
     insert into one_to_one values(new.message_id,new.reciever_id,new.read); 
    ELSE 
     select count(*) into t from parent_master where parent_id=new.reciever_id; 
     IF t!=0 THEN 
      insert into one_to_one values(new.message_id,new.reciever_id,new.read); 
     END IF; 
    END IF; 
END 

EDIT:以上是Oracle解決方案,因爲每OP的標籤。對於MySQL,該DECLARE語法稍有不同:

CREATE TRIGGER check_reciever BEFORE INSERT 
ON one_to_one FOR each row 
BEGIN 
    -- Declarations 
    DECLARE temp NUMERIC; 
    DECLARE t NUMERIC; 
    -- End declarations 
    select employee_id into temp from employee_master where employee_id = NEW.reciever_id; 
    IF temp!=0 THEN 
     insert into one_to_one values(new.message_id,new.reciever_id,new.read); 
    ELSE 
     select count(*) into t from parent_master where parent_id=new.reciever_id; 
     IF t!=0 THEN 
      insert into one_to_one values(new.message_id,new.reciever_id,new.read); 
     END IF; 
    END IF; 
END 
+0

您的解決方案給了我以下錯誤:MySQL表示:文件 #1064 - 你在你的SQL語法錯誤;請檢查對應於您的MySQL服務器版本的手冊,以找到正確的語法,以便在第4行'DECLARE temp NUMBER'處使用 – smily

+0

您的文章標記爲[tag:oracle]和[tag:plsql],而不是[tag:mysql] ,所以我回答Oracle。讓我們嘗試鞭打一些... – Mureinik

+0

...在這裏,檢查出 – Mureinik