문제

Table structure is:

mysql> DESC groups;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| PKey         | varchar(64)  | NO   | PRI | NULL    |       |
| group_name   | varchar(64)  | YES  |     | NULL    |       |
| Region       | varchar(128) | NO   |     | NULL    |       |
| Role         | varchar(128) | NO   |     | NULL    |       |
| parent_group | varchar(64)  | NO   | MUL | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

When i am executing this Trigger , i'm having a compilation error

DELIMITER $$
CREATE
    TRIGGER `group_before_delete` BEFORE DELETE
    ON `groups`
    FOR EACH ROW BEGIN
    IF old.parent_group=old.PKey THEN
        UPDATE `Error: deletion RootGroup is prohibited!`;
    ELSE              
        UPDATE groups
        SET parent_group=old.parent_group
        WHERE parent_group=old.Pkey; 
    END IF;
    END$$
DELIMITER ;

ERROR 1064 (42000): 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 ';
ELSE
t_group=old.parent_group
t_group=old.PKey;
END IF;' at line 6
mysql> DELIMITER ;


Can you tell me what i'm missing here ??

도움이 되었습니까?

해결책 2

In your IF statement, the following is not a valid SQL statement:

UPDATE `Error: deletion RootGroup is prohibited!`;

This should be this:

IF old.parent_group=old.PKey THEN
    UPDATE `Error: deletion RootGroup is prohibited!` set x=1;
ELSE              
    UPDATE groups
    SET parent_group=old.parent_group
    WHERE parent_group=old.PKey; 
END IF;

I have never done things this way. It is a bit of an ugly way of doing it. But if it works, what the heck.

다른 팁

You get an error in your code because the syntax of your UPDATE statement is not valid.

The links you give are 4 and 5 years old! Since the SIGNAL statement is available in MySQL since version 5.5.0 (released 3 years ago), this is really not a good idea to use the hacks described in these 2 webpages. Instead, use the SIGNAL statement.

Note: From the comments we learn that the OP is not using MySQL 5.5, so SIGNAL is not available.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top