日期:2014-05-16  浏览次数:20751 次

[求助]MYSQL创建触发器遇到很奇怪的问题。
今天遇到一个很奇怪的问题,是关于创建触发器的。创建触发器代码如下
SQL code

create trigger trigger_t_test_U
after update
on t_test
for each row
begin
insert into t_syn_data(table_name,event_type,pk_data,create_time,from_source)
values('t_test','U',NEW.id,now(),'127.0.0.1_mysql_server');
set @key=LAST_INSERT_ID();
insert into t_syn_value(syndata_id,filed_name,filed_value,filed_struct,filed_file,filed_type)
values(@key,'name',NEW.name,'0','-1','1');
end;


在MYSQL中对表t_test创建触发器时,提示第7行有错“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 '' at line 7”。
可是我找了半天都没看明白,到底是哪里错了呢?


------解决方案--------------------
set @key=LAST_INSERT_ID();


select LAST_INSERT_ID() into @key;
------解决方案--------------------
那直接用MYSQL的命令行工具试一下,
引用
delimiter $
create trigger trigger_t_test_U
after update
on t_test
for each row
begin
insert into t_syn_data(table_name,event_type,pk_data,create_time,from_source)
values('t_test','U',NEW.id,now(),'127.0.0.1_mysql_server');
select LAST_INSERT_ID() into @key;
insert into t_syn_value(syndata_id,filed_name,filed_value,filed_struct,filed_file,filed_type)
values(@key,'name',NEW.name,'0','-1','1');
end;
delimiter;