error in trigger (mysql)

时间:2015-11-12 10:37:57

标签: mysql triggers

I am trying to create a trigger with an IF condition and I am getting an error on line 6.

DELIMITER $$

CREATE TRIGGER trgSoftwareLicenseDetails
AFTER UPDATE ON SoftwareLicenseDetails
FOR EACH ROW BEGIN
IF new.Flag = 0 THEN
    INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', (SELECT Title FROM SoftwareTypes WHERE ID = 
    (SELECT SoftwareNameFK FROM SoftwareLicenseDetails
    WHERE  ComputerFK=new.ComputerFK
    ORDER BY ID Desc
    LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
FROM SoftwareLicenseDetails
WHERE  ComputerFK=new.ComputerFK
ORDER BY ID Desc
LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
ELSE IF new.Flag = 1 THEN
    INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', 'N/A', (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Deleted',old.ComputerFK);
END IF;
END; $$
DELIMITER ;

Error:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM SoftwareLicenseDetails WHERE ComputerFK=new.ComputerFK ORDER BY ID Desc LI' at line 6 0.000 sec

What am I missing?

1 个答案:

答案 0 :(得分:1)

Check this:

DELIMITER $$

CREATE TRIGGER trgSoftwareLicenseDetails
AFTER UPDATE ON SoftwareLicenseDetails
FOR EACH ROW 

BEGIN

    IF new.Flag = 0 THEN
        INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', (SELECT Title FROM SoftwareTypes WHERE ID = 
        (SELECT SoftwareNameFK FROM SoftwareLicenseDetails
        WHERE  ComputerFK=new.ComputerFK
        ORDER BY ID Desc
        LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);

    ELSEIF new.Flag = 1 THEN
        INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', 'N/A', (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Deleted',old.ComputerFK);
    END IF;

END; $$
DELIMITER ;