2016-09-26 151 views
0

我使用的是trigger在admitted_student using mysql. This is my trigger`代碼插入火後:爲什麼這個觸發器沒有執行?

DELIMITER // 

CREATE TRIGGER Record_after_insert 
AFTER INSERT 
    ON admitted_student FOR EACH ROW 

BEGIN 

    DECLARE who varchar(50); 
    DECLARE IDD int (15); 
    SELECT admitted_student_id FROM admitted_student ORDER BY admitted_student_id DESC into IDD; 
    SELECT USER() INTO who; 
    INSERT INTO student_audit 
    (id, 
    attime, 
    who) 
    VALUES 
    (IDD, 
    SYSDATE(), 
    who); 
END; 
DELIMITER ; 

這是我的桌子上,我使用觸發器:

enter image description here

而且這個我的觸發表:

enter image description here

當我執行觸發器代碼,然後它給了我下面的錯誤:

enter image description here

+0

您將分隔符更改爲'//',但從不使用它。 –

+0

你想要做什麼,首先選擇stmt? – Drew

+0

iirc合適的更現實的格式將是'SELECT acceptance_student_id INTO IDD FROM admission_student ORDER BY admit_student_id DESC LIMIT 1;'將範圍限制爲零或一個值,否則該東西將以您擁有的方式爆炸 – Drew

回答

1

對於非的phpMyAdmin嘗試:

DROP TRIGGER IF EXISTS Record_after_insert; 
DELIMITER // 
CREATE TRIGGER Record_after_insert 
AFTER INSERT 
    ON admitted_student FOR EACH ROW 

BEGIN 
    INSERT INTO student_audit (action,id,attime,who) 
    SELECT 'something',NEW.admitted_student_id,now(),CURRENT_USER(); 
END; // 
DELIMITER ; 

爲PHPMyAdmin的嘗試:

DROP TRIGGER IF EXISTS Record_after_insert; 
CREATE TRIGGER Record_after_insert 
AFTER INSERT 
    ON admitted_student FOR EACH ROW 

BEGIN 
    INSERT INTO student_audit (action,id,attime,who) 
    SELECT 'something',NEW.admitted_student_id,now(),CURRENT_USER(); 
END; 

的phpMyAdmin沒有按用戶a DELIMITERCURRENT_USER()是您在登錄時解決的用戶。

who VARCHAR(10)應該像VARCHAR(100)一樣。

attime應該是DATETIME不是它是什麼。

+0

PHPMYAdmin代碼中第7行的語法錯誤。 –