开发者

MySql Trigger won't compute

开发者 https://www.devze.com 2023-02-06 05:00 出处:网络
I need to create a trigger that computes one column value based on other column values. A database is a little bit denormalized to get higher performance. (Normalization is not an issue of this questi

I need to create a trigger that computes one column value based on other column values. A database is a little bit denormalized to get higher performance. (Normalization is not an issue of this question).

The problem is that I want to set value of computed_address value and it is ok if i put a constant in it. But it seems that these If 开发者_Go百科clauses aren't working and I just can't see the problem.

Below is the trigger code. Thank you very much!

DELIMITER $$

USE `nth_poi_new_3`$$

DROP TRIGGER /*!50032 IF EXISTS */ `poi_address_creator`$$

CREATE
    /*!50017 DEFINER = 'root'@'localhost' */
    TRIGGER `poi_address_creator` BEFORE INSERT ON `poi` 
    FOR EACH ROW BEGIN
    DECLARE full_address VARCHAR(255);
    DECLARE country_string VARCHAR(100);
    DECLARE region_string VARCHAR(100);
    DECLARE town_string VARCHAR(100);
    DECLARE address_string VARCHAR(100);

    IF NEW.address <> '' THEN   
        SET full_address = CONCAT(NEW.address, ",");

    END IF;
    IF NEW.town_name IS NOT NULL THEN
        SET full_address = CONCAT(full_address, NEW.town_name, ",");
    ELSEIF NEW.town_id IS NOT NULL THEN
        SELECT NAME INTO town_string FROM town WHERE town.town_id = NEW.town_id LIMIT 1;
        SET full_address = CONCAT(full_address, town_string, ",");
    END IF;

    IF NEW.region_name IS NOT NULL THEN
        SET full_address = CONCAT(full_address, NEW.region_name, ",");
    ELSEIF NEW.region_id IS NOT NULL THEN
        SELECT NAME INTO region_string FROM region WHERE region.region_id = NEW.region_id LIMIT 1;
        SET full_address = CONCAT(full_address, region_string, ",");
    END IF;

    IF NEW.country_name IS NOT NULL THEN
        SET full_address = CONCAT(full_address, NEW.country_name, ",");
    ELSEIF NEW.country_id IS NOT NULL THEN
        SELECT NAME INTO country_string FROM country WHERE country.country_id = NEW.country_id LIMIT 1;
        SET full_address = CONCAT(full_address, country_string, ",");
    END IF;

    SET NEW.computed_address = full_address;

    END;
$$

DELIMITER ;


at the bottom of your trigger code

END; 
$$

DELIMITER ;

should be

END$$

DELIMITER ;


Hey, just to answer. I had to initialize full_address variable first. I just made this:

DECLARE full_address VARCHAR(255);

But after declaration it needed to be initialization also:

SET full_address="";
0

精彩评论

暂无评论...
验证码 换一张
取 消