我正在尝试使用用于nodejs的mysql包为数据库创建触发器。但我说
时出错您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在\'DELIMITER $$ CREATE TRIGGER'NotificationTrigger'附近第1行'INSERT ON'Leave \'之后使用。
var createNotificationTrigger = {
query:
'DELIMITER $$\
CREATE TRIGGER `NotificationTrigger` \
After INSERT ON `Leave` \
FOR EACH ROW \
BEGIN \
Declare fullname varchar(255); \
Declare lastname varchar(255); \
Declare requestedbyid int; \
Declare insertstring varchar(255); \
Set fullname = ( select Employee.FirstName from Employee where Employee.ID = NEW.RequestedByID); \
Set lastname = ( select Employee.LastName from Employee where Employee.ID = NEW.RequestedByID); \
set fullname = (select Concat(fullname," ",lastname)); \
set requestedbyid= (select New.RequestedByID); \
set insertstring = (select Concat("New Leave request from ",fullname , " employee id : ",requestedbyid)); \
INSERT INTO `Notification` \
(`Title`,`SeenBy`,`SentByID`,`NotificationTypeID`) \
VALUES(insertstring, requestedbyid, requestedbyid,2) ; \
END$$ \
DELIMITER ;'
};
这是我的触发器查询的样子。我正在使用:
connection.query(createNotificationTrigger.query,[],(err)=>{//something here});
这应该在我的数据库中创建一个触发器,但是引发了上述错误。该查询在mysql工作台中正常工作。
答案 0 :(得分:0)
根据this answer,通过删除DELIMITER
语句,您应该能够创建触发器而不会出错。我已经使用自己的模式和自己的触发器进行了测试,并且可以正常工作。
因此,您的代码应修改为以下内容以使其起作用:
var createNotificationTrigger = {
query:
'CREATE TRIGGER `NotificationTrigger` \
After INSERT ON `Leave` \
FOR EACH ROW \
BEGIN \
Declare fullname varchar(255); \
Declare lastname varchar(255); \
Declare requestedbyid int; \
Declare insertstring varchar(255); \
Set fullname = ( select Employee.FirstName from Employee where Employee.ID = NEW.RequestedByID); \
Set lastname = ( select Employee.LastName from Employee where Employee.ID = NEW.RequestedByID); \
set fullname = (select Concat(fullname," ",lastname)); \
set requestedbyid= (select New.RequestedByID); \
set insertstring = (select Concat("New Leave request from ",fullname , " employee id : ",requestedbyid)); \
INSERT INTO `Notification` \
(`Title`,`SeenBy`,`SentByID`,`NotificationTypeID`) \
VALUES(insertstring, requestedbyid, requestedbyid,2) ; \
END'
};