2013-07-21 3 views
0

내 MySQL의 트리거선언하고 트리거

DELIMITER // 
CREATE TRIGGER lestrigger 
    AFTER INSERT ON examinations 
    FOR EACH ROW 
    BEGIN 
    DECLARE the_last_inserted_id INT ; 
    DECLARE the_class_id INT; 
    DECLARE the_year_id INT; 

    SET the_last_inserted_id = LAST_INSERT_ID(); 
    SET the_class_id = select examination_class_id from examinations where examination_id = the_last_inserted_id; 
    SET the_year_id = select examination_class_year_id from examinations where examination_id = the_last_inserted_id; 

    insert into examination_data (ed_cs_id,ed_examination_id) select cs_id from class_students where cs_class_id = the_class_id AND cs_year_id = the_year_id,the_last_inserted_id; 

END 
    // 
DELIMITER ; 

몇 가지 변수를 선언하고하지만 난이 오류를

/* SQL 오류 (1064)가 계속 변수를 사용 : 당신은 오류가 귀하의 SQL 구문; 라인 10 */

에서 내가 구문 내 변수를 사용하고있는 방법이 있나요 근처 사용할 수있는 권리 구문 MySQL 서버 버전에 해당하는 설명서를 확인 'examination_id = the_last_in 어디 시험 에서 examination_class_id 선택' 권리?.

+0

를 추가해보십시오 괄호. – Hidde

+0

http://seackoverflow.com/questions/12797424/selecting-values-to-a-variable-inside-mysql-triggers – mostruash

답변

0

사용 INTO 키워드 선택 쿼리 주위 http://dev.mysql.com/doc/refman/5.0/en/select-into.html

select examination_class_id into the_class_id from examinations where examination_id = the_last_inserted_id; 
select examination_class_year_id into the_year_id from examinations where examination_id = the_last_inserted_id; 
+0

그 역시 효과가 있었지만이'insert into examination_data (ed_cs_id, ed_examination_id) (select cs_id from class_students where cs_class_id = the_class_id and the cs_year_id = the_year_id), the_last_inserted_id);'나는 여분의 열에 삽입하려고합니다. 내가'('및')'?와 함께 select 문을 둘러 쌉니다. –

+0

괄호에 대해 확실하지 않지만 쿼리에 나쁜 영향을 줄 수는 없습니다. 확실하지 않으면 추가하십시오. 그렇지 않으면 데이터 이전에 누락 된 키워드 'VALUES'를 제외하고 쿼리가 정확합니다. – sdespont