TRIGGER MYSQL不存在

时间:2013-09-25 10:24:05

标签: mysql

我创建了这个触发器:

    CREATE TABLE `Invitations` 
( `userId` varchar(255) NOT NULL, `name` varchar(255) DEFAULT NULL, 
    `status` varchar(1) DEFAULT NULL, `userId_inv` varchar(255) NOT NULL, 
    PRIMARY KEY (`userId`,`userId_inv`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1


CREATE TRIGGER `Invitation_001_after_trig` AFTER INSERT ON `invitation_001`
FOR EACH ROW
BEGIN  
    IF ((new.userId,'001') NOT EXISTS (SELECT `userId`,`userId_inv` FROM `Invitations` 
    WHERE `userId`= new.userId AND `userId_inv` = '001' LIMIT 1))
    THEN 
    INSERT INTO `Invitations`(`userId`, `name`,`status`,`userId_inv`) 
    values (new.userId, new.name,new.status,'001'); 
    DELETE FROM `invitation_001` WHERE `status` = 'a';
    END IF;
END;

它不起作用。我有这个错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近'EXISTS(SELECT userIduserId_inv FROM Invitations WHERE   第1行userId = new.user'

你能帮助我吗?

我用这个触发器解决了这个问题:

CREATE TRIGGER `Invitation_001_after_trig` AFTER INSERT ON `invitation_001` 
FOR EACH ROW 
BEGIN  
INSERT INTO `Invitations` (`userId`, `name` ,`status` ,`userId_inv`) SELECT * FROM    
invitation_001 iv WHERE 
NOT EXISTS (SELECT 1 FROM `Invitations` ivs WHERE ivs.`userId` = iv.`userId` AND 
ivs.`userId_inv` = iv.`userId_inv`) AND iv.`userId_inv`='001';END;

现在我还有另外一个问题。我想在触发器中执行来自Concat字符串的查询,但直到现在我没有成功,所以这是我的代码:

CREATE TRIGGER `InvitationS_after_trig` AFTER INSERT ON `Invitations` 
FOR EACH ROW 
BEGIN 

    if(new.status ='c' OR new.status ='r')
    BEGIN
        SET @query = CONCAT("INSERT INTO", Invetations.userId,"values(new.userId,new.name,new.status,new.userId_inv);");
        PREPARE stmt1 FROM @query; 
        EXECUTE stmt1; 
        DEALLOCATE PREPARE stmt1;
    END IF;
END;

我解决了语法问题,但我现在还有另外一个:

  

存储函数或触发器

中不允许使用动态SQL

我使用的是MySQL 5.1.69。

最后我放弃了,我将使用java Fonction解决这个问题

0 个答案:

没有答案