我也有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


感谢大家的帮助

最佳答案

试试这个脚本-

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

09-12 11:00