MySQL:触发器的用户定义函数

时间:2018-11-15 06:25:54

标签: mysql sql triggers innodb

我需要编写一个困难的触发器以在数据集中实施一个奇怪的模式,因此我决定将工作分解为几个函数。但是,我遇到了一些我不理解的错误:

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 '`Product Name`;
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 'STRING     BEGIN   SET @temp_query = (SELECT s1.`Product Name` FROM Sells s1 WHE' 

我的代码:

/* DEFINE VARIABLES */
SET @above_beer_name = null;
SET @below_beer_name = null;
SET @below_beer_average = null;
SET @above_beer_average = null;
SET @average_below_above = null;
SET @temp_query = null;

/* Function gets one beer higher than the input Sells entry in price*/
DELIMITER $$
CREATE FUNCTION getAboveBeer() RETURNS STRING
    BEGIN
        SET @temp_query = (SELECT s1.`Product Name`
             FROM Sells s1
             WHERE s1.`Bar Name` = NEW.`Bar Name`
               AND NEW.`Price` <= s1.`Price`
             ORDER BY `Price` ASC LIMIT 1);
        SET @above_beer_name = @temp_query.`Product Name`;
        RETURN @above_beer_name;
    END$$



/* Function gets one beer lower than the input Sells entry in price*/
DELIMITER $$    
CREATE FUNCTION getBelowBeer() RETURNS STRING
BEGIN
    SET @temp_query = (SELECT Sells.`Product Name` FROM Sells
           WHERE Sells.`Bar Name` = NEW.`Bar Name`
             AND NEW.`Price` >= s1.`Price`
           ORDER BY ASC LIMIT 1);
    SET @below_beer_name = @temp_query.`Product Name`;
    RETURN @below_beer_name;
END $$;

0 个答案:

没有答案