Mysql - Concat - 过程执行失败

时间:2012-06-18 12:10:29

标签: mysql triggers procedure concat

我也有123张桌子:M001 - > M010 - > M123。 每个人都是客户。当它到达父表的记录时,触发器调用如下函数:

  Declare MasterX  int;
  Set MasterX = New.Master;
  Call Lecturas_Insertar(MasterX,New.Id);

这是我的功能:

BEGIN

#Set Master
  If MasterX < 10 Then 
  Set MasterX = Concat("lecturas.M00",MasterX);
  End If;
#Set Master
  If MasterX Between 10 and 99 Then 
  Set MasterX = Concat("lecturas.M0",MasterX);
  End If;

  set @a=concat("INSERT INTO ",MasterX, "(Id) Values(" ,Id, ")");
  PREPARE stmt1 FROM @a;
  EXECUTE stmt1; 
  DEALLOCATE PREPARE stmt1;

END

但它总是抛出以下错误:

  Procedure execution failed
  1146 - Table 'lecturas.M' does not exist

感谢所有人的帮助

1 个答案:

答案 0 :(得分:1)

试试这个脚本 -

BEGIN
  SET @a = CONCAT('INSERT INTO lecturas.M', LPAD(MasterX, 3, 0), '(Id) Values(', Id, ')');
  PREPARE stmt1 FROM @a;
  EXECUTE stmt1; 
  DEALLOCATE PREPARE stmt1;
END