在mysql触发器中声明变量

时间:2015-06-23 18:04:08

标签: mysql variables encryption triggers

我有一个MySQL触发器,用于调用rsaencrypt函数来加密特定值。

DELIMITER $$

CREATE TRIGGER ssninsertencrypt BEFORE INSERT ON redcap_data
FOR EACH ROW
BEGIN
    IF new.project_id = (SELECT ProjectID FROM redcap_encryption)
    AND new.field_name = (SELECT FieldName FROM redcap_encryption)
        THEN
            SET @PublicKey = (SELECT PublicKey FROM redcap_encryption WHERE     ProjectID = new.project_id);
            SET new.value = rsaencrypt(new.value,@PublicKey);
    END IF;
END$$

DELIMITER ;

这似乎是不一致的工作/不工作所以我想将触发器生成的已完成语句插入到另一个表中,这样就可以看到传递给rsaencrypt的是什么或者没有传递。我以为我可以像以下一样做一个SET @SQL()

SET @SQL = 
(IF new.project_id = (SELECT ProjectID FROM redcap_encryption)
AND new.field_name = (SELECT FieldName FROM redcap_encryption)
    THEN
        SET @PublicKey = (SELECT PublicKey FROM redcap_encryption WHERE ProjectID = new.project_id);
        SET new.value = rsaencrypt(new.value,@PublicKey);
END IF;)

我在这方面遇到语法错误,我不确定如何最好地继续

由于

1 个答案:

答案 0 :(得分:0)

前一段时间解决了这个问题,但忘了发帖: -

BEGIN
    IF new.field_name = (SELECT FieldName FROM redcap_encryption WHERE new.project_id = ProjectID)
        THEN
            SET @PublicKey = (SELECT PublicKey FROM redcap_encryption WHERE ProjectID = new.project_id);
            SET new.value = rsaencrypt(new.value,@PublicKey);
    END IF;
END$$