如何使用mysql在触发器中声明变量?

时间:2015-11-18 09:12:03

标签: mysql database rdbms

CREATE OR REPLACE TRIGGER ATTENDANCE_NOTIFY AFTER INSERT OR UPDATE ON ATTENDANCE
FOR EACH ROW
DECLARE 
    V_STUDENT_ID    STUDENT.STUDENT_ID%TYPE := NULL;
    V_HOD_ID       HEAD_OF_DEPARTMENT.HOD_ID%TYPE := NULL;
    V_SUBCODE STUDENT.SUBCODE%TYPE := NULL;
    V_ATTENDANCE    ATTENDENCE%TYPE := NULL;
BEGIN
    SELECT SUB_CODE, SUB_NAME INTO V_SUB_CODE,  FROM SUBJECT WHERE STUDENT_ID = :NEW.STUDENT_ID;
    SELECT STUDENT_ID INTO V_STUDENT_ID FROM STUDENT WHERE SUBJECT_CODE = :NEW.SUBJECT_CODE;
    SELECT HOD_ID INTO V_HOD_ID FROM STUDENT_HOD WHERE STUDENT_ID = :NEW.STUDENT_ID;
    SELECT ATTENDENCE INTO V_ATTENDENCE FROM ATTENDENCE WHERE STUDENT_ID=:NEW_STUDENT_ID
    IF (V_ATTENDENCE IS NOT NULL AND V_SUB_CODE IS NOT NULL AND V_STUDENT_ID IS NOT NULL AND V_HOD_ID IS NOT NULL) THEN
     IF (:NEW.ATTENDENCE < (V_ATTENDENCE * 0.85)) THEN
        INSERT INTO NOTIFY VALUES(V_HOD_ID, V_STUDENT_ID || ' (ID:- ' || :NEW.STUDENT_ID ||') HAS LESS THAN 85%  ATTENDENCE IN SUBJECT ' || V_SUB_CODE);
        END IF;
    END IF;    
EXCEPTION
WHEN OTHERS
   THEN NULL;
END;   

我在声明

中遇到语法错误

1 个答案:

答案 0 :(得分:0)

没有办法在MySQL中引用列的数据类型。 DECLARE必须静态声明变量的类型和大小。

这样的东西
DECLARE myvar VARCHAR( 8 ) -- This is valid in Mysql

DECLARE myvar mytable.myfield%TYPE    --This is invalid in Mysql

希望这有帮助。