从Shell脚本调用mysql触发器的问题?

时间:2014-05-07 12:56:40

标签: mysql triggers

mysql -h $host -u $user -p$pass $database -e "Create trigger trgQueuedSms
                         AFTER DELETE ON tblSmsQueue
FOR EACH ROW
BEGIN
DECLARE VALUE INTEGER;
SET value := (SELECT SUBSTRING_INDEX(USER(),'@',1)='user') ;
IF (value > 0)
THEN
INSERT INTO tblSmsQueueArchive(iID, sMsgId, sUserName, sResellerName,sDistributorName, sMsgText, sType, sSender, sDestination, bDlr, dtLoggedTime, iJobId, sFailReason) VALUES    (old.iID, old.sMsgId, old.sUserName, old.sResellerName,old.sDistributorName,old.sMsgText, old.sType, old.sSender, old.sDestination, old.bDlr, NOW(), old.iJobId, 'DELETED');
END IF;
END;"

1 个答案:

答案 0 :(得分:0)

由于你的触发器定义中有分号,你应该声明DELIMITER不是分号,而是在DELIMITER之后使用END。例如,您可以使用$$作为DELIMITER,如下所示:

mysql -h $host -u $user -p$pass $database --delimiter="$$" -e "Create trigger trgQueuedSms
                         AFTER DELETE ON tblSmsQueue
FOR EACH ROW
BEGIN
DECLARE VALUE INTEGER;
SET value := (SELECT SUBSTRING_INDEX(USER(),'@',1)='user') ;
IF (value > 0)
THEN
INSERT INTO tblSmsQueueArchive(iID, sMsgId, sUserName, sResellerName,sDistributorName, sMsgText, sType, sSender, sDestination, bDlr, dtLoggedTime, iJobId, sFailReason) 
VALUES    (old.iID, old.sMsgId, old.sUserName, old.sResellerName,old.sDistributorName,old.sMsgText, old.sType, old.sSender, old.sDestination, old.bDlr, NOW(), old.iJobId, 'DELETED');
END IF;
END $$"