CREATE FUNCTION语法错误@postLimit(在文本后缀处)

时间:2015-06-01 13:28:26

标签: mysql mysql-workbench

我有这个SQL代码来创建函数:

CREATE FUNCTION  `inHashtagCampaign` (campaignId INT,postDateTime INT UNSIGNED, postSource CHAR(10))
RETURNS BOOLEAN
LANGUAGE SQL
DETERMINISTIC
BEGIN 
    DECLARE @postLimit INT(11);
    DECLARE @active OOLEAN;
    DECLARE @postCount BIGINT(50);
    DECLARE @suspendOnLimit TINYINT(1);
    DECLARE returnVal BOOLEAN;
    DECLARE @trackingTwitter BOOLEAN;
    DECLARE @trackingInstagram BOOLEAN;
    DECLARE @trackingYoutube BOOLEAN;
    SELECT
        @postLimit := `c`.`postLimit`,
        @trackingInstagram := `c`.`channelInstagram`,
        @trackingTwitter := `c`.`channelTwitter`,
        @trackingYoutube := `c`.`channelYoutube`,
        @active := IF(
            (postDateTime BETWEEN 
                UNIX_TIMESTAMP(`c`.`startTracking`) AND
                UNIX_TIMESTAMP(`c`.`endTracking`)
            )
            AND `c`.`suspended` != 1,TRUE,FALSE
        ),
        @suspendOnLimit := IF(
            `c`.`suspendOnLimit` = 1, TRUE, FALSE
        ),
        @postCount := `cp`.`posts`
    FROM `campaigns` `c`
    LEFT JOIN `collectedPosts` `cp`
        ON `c`.`id` = `cp`.`hashtagId`
    WHERE `c`.`id` = campaignId
        AND `cp`.`source` = '';
    call debug_msg("active: ",@active);
    IF active = TRUE AND (postCount < postLimit AND suspendOnLimit = FALSE)
    THEN
        SET returnVal = TRUE;
    ELSE
        SET returnVal = FALSE;
    END IF;
    IF returnVal = TRUE THEN
        CASE postSource
            WHEN 'twitter' AND trackingTwitter != 1 THEN
                SET returnVal = FALSE;
            WHEN 'instagram' AND trackingInstagram != 1 THEN
                SET returnVal = FALSE;
            WHEN 'youtube' AND trackingYoutube != 1 THEN
                SET returnVal = FALSE;
        END CASE;
    END IF;
    RETURN returnVal;
END;

我知道分隔符,所有内容都丢失了,它是由mysql workbench生成的。 我遇到的问题是我收到了这个错误:

  

语法错误:意外&#39; @ postLimit&#39; (在文本后缀处。)

导致此问题的原因是什么?

2 个答案:

答案 0 :(得分:0)

这里你犯了一个错误DECLARE @active OOLEAN;应该是BOOLEAN

答案 1 :(得分:0)

使用@时,您不必使用DECLARE。删除@或跳过DECLARE

此外,OOLEAN不是数据类型。它应该是BOOLEAN

所以它看起来应该是这样的(我也更改了代码,因为SELECT会返回一个集合,您使用INTO

CREATE FUNCTION  `inHashtagCampaign` (campaignId INT,postDateTime INT UNSIGNED, postSource CHAR(10))
RETURNS BOOLEAN
LANGUAGE SQL
DETERMINISTIC
BEGIN 
    DECLARE postLimit INT(11);
    DECLARE active BOOLEAN;
    DECLARE returnVal BOOLEAN;
    DECLARE postCount BIGINT(50);
    DECLARE suspendOnLimit TINYINT(1);
    DECLARE trackingTwitter BOOLEAN;
    DECLARE trackingInstagram BOOLEAN;
    DECLARE trackingYoutube BOOLEAN;
    SELECT
         `c`.`postLimit`,
         `c`.`channelInstagram`,
         `c`.`channelTwitter`,
         `c`.`channelYoutube`,
         IF(
            (postDateTime BETWEEN 
                UNIX_TIMESTAMP(`c`.`startTracking`) AND
                UNIX_TIMESTAMP(`c`.`endTracking`)
            )
            AND `c`.`suspended` != 1,TRUE,FALSE
        ),
        suspendOnLimit = IF(
            `c`.`suspendOnLimit` = 1, TRUE, FALSE
        ),
        postCount = `cp`.`posts`
    FROM `campaigns` `c` 
    LEFT JOIN `collectedPosts` `cp`
        ON `c`.`id` = `cp`.`hashtagId` 
    WHERE `c`.`id` = campaignId
        AND `cp`.`source` = '' INTO postLimit,trackingInstagram,trackingTwitter,trackingYoutube,active;
    call debug_msg("active: ",@active);
    IF active = TRUE AND (postCount < postLimit AND suspendOnLimit = FALSE)
    THEN
        SET returnVal = TRUE;
    ELSE
        SET returnVal = FALSE;
    END IF;
    IF returnVal = TRUE THEN
        CASE postSource
            WHEN 'twitter' AND trackingTwitter != 1 THEN
                SET returnVal = FALSE;
            WHEN 'instagram' AND trackingInstagram != 1 THEN
                SET returnVal = FALSE;
            WHEN 'youtube' AND trackingYoutube != 1 THEN
                SET returnVal = FALSE;
        END CASE;
    END IF;
    RETURN returnVal;
END;