Mysql

觸發附近信號語句中的 concat 語法錯誤

  • November 18, 2015

請幫助我了解 MySQL 5.6 的語法。

出於某種原因,我得到了錯誤:

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 '('WRONG SOCIAL: ', new.social);
       end if;
       if not (new.photo is nul'

嘗試執行此 SQL 程式碼時:

create table table_social (
       sid varchar(250) primary key,
       auth char(32) not null,
       social integer,               -- to be checked by triggers
       female boolean,
       given varchar(250) not null,
       family varchar(250) null,
       photo varchar(1000) null,     -- to be checked by triggers
       place varchar(250) null,
       ip varchar(250) not null,
       uid integer not null references 
           table_users(uid) on delete cascade
);

delimiter //

create trigger social_insert_trigger before insert on table_social
for each row begin
       if new.social < 0 OR new.social > 6 then
               signal sqlstate '45000' 
               set message_text = concat('WRONG SOCIAL: ', new.social);
       end if;
       if not (new.photo is null OR new.photo regexp '^https?://') then
               signal sqlstate '45000' 
               set message_text = concat('WRONG PHOTO: ', new.photo);
       end if;
end
//

create trigger social_update_trigger before update on table_social
for each row begin
       if new.social < 0 OR new.social > 6 then
               signal sqlstate '45000' 
               set message_text = concat('WRONG SOCIAL: ', new.social);
       end if;
       if not (new.photo is null OR new.photo regexp '^https?://') then
               signal sqlstate '45000' 
               set message_text = concat('WRONG PHOTO: ', new.photo);
       end if;
end
//

我嘗試刪除concat,用雙引號替換單引號,將signal行分成兩部分(如上)並返回 - 仍然無法找出問題所在。

測試這段程式碼:

drop table if EXISTS table_social;
create table table_social (
       sid varchar(250) primary key,
       auth char(32) not null,
       social integer,               -- to be checked by triggers
       female boolean,
       given varchar(250) not null,
       family varchar(250) null,
       photo varchar(1000) null,     -- to be checked by triggers
       place varchar(250) null,
       ip varchar(250) not null,
       uid integer not null references 
           table_users(uid) on delete cascade
);

delimiter //

create trigger social_insert_trigger before insert on table_social
for each row begin
       if new.social < 0 OR new.social > 6 then
               set @message_text = concat('WRONG SOCIAL: ', new.social);
               signal sqlstate '45000'
               set MESSAGE_TEXT = @message_text;
       end if;
       if not (new.photo is null OR new.photo regexp '^https?://') then
               set @message_text = concat('WRONG PHOTO: ', new.photo);
               signal sqlstate '45000' set message_text = @message_text;
       end if;
end
//

create trigger social_update_trigger before update on table_social
for each row begin
       if new.social < 0 OR new.social > 6 then
               set @message_text = concat('WRONG SOCIAL: ', new.social);
               signal sqlstate '45000' 
               set message_text = @message_text;
       end if;
       if not (new.photo is null OR new.photo regexp '^https?://') then
               set @message_text = concat('WRONG PHOTO: ', new.photo);
               signal sqlstate '45000' 
               set message_text = @message_text;
       end if;
end
//

它不理解SIGNAL SQLSTATE SET message_text中的CONCAT()

引用自:https://dba.stackexchange.com/questions/121465